Skip to content

Conversation

@TyTodd
Copy link

@TyTodd TyTodd commented Nov 3, 2025

I replaced student: Module in Teleprompt.compile's signature with student: M, a TypeVar that tracks the type and returns that same type

M = TypeVar("M", bound=Module)
class Teleprompter:
...
    def compile(
        self,
        student: M,
        ...

I also applied this signature to all other compilers in the SDK.

It doesn't change the functionality of anything at all. My main reason for adding this is I (and others) often add custom functionality to dspy.Modules and its just convenient to have the type checker still recognize those functions and attributes on the optimized program after running compile.

For example, in this script the type checker will not highlight weather_function because the custom function is forgotten when the new program is returned from compile. With this new commit that is no longer the case.

class MyAgent(Module):
    def __init__(self):
        super().__init__()
        self.predict = dspy.ReAct("question -> answer", tools=[self.weather_function])

    def weather_function(self):
        return random.random()

optimizer = MIPROv2(metric=dspy.evaluate.answer_exact_match, auto="light")
compiled_agent = optimizer.compile(student=MyAgent(), trainset=trainset, teacher=teacher, valset=valset)
print(compiled_agent.weather_function())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant