Skip to content

Conversation

@dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Nov 3, 2025

Right now, the variance of RunContext is the reverse of what it should be, because we are reusing a typevar rather than creating a type-specific one. Because of this, you can't define reusable tools that use deps but aren't exact matches on the agent deps type. This change fixes the typing (it is actually incorrect, this is a typing bugfix not just a change), and in particular makes it work to define tools as accepting supertypes of the agent's actual deps class, as you can see in the new typed_deps.py file I added as a typing test.

(I recall pointing out the potential for reusing typevars to cause issues when we first did it; it won't surprise me if it remains an issue. In general I would strongly prefer to use infer_variance if it wasn't for the fact that it makes it impossible to have dataclasses in 3.13 that are covariant due to the __replace__ method.)

This addresses the issue described in this thread: https://pydanticlogfire.slack.com/archives/C083V7PMHHA/p1762185662720909

@github-actions
Copy link

github-actions bot commented Nov 3, 2025

Docs Preview

commit: 7ab0a5b
Preview URL: https://9b2a62a3-pydantic-ai-previews.pydantic.workers.dev

@dmontagu dmontagu enabled auto-merge (squash) November 3, 2025 21:29
@dmontagu dmontagu merged commit d16f634 into main Nov 3, 2025
30 checks passed
@dmontagu dmontagu deleted the dmontagu/fix-typevar-variance-for-agent-deps branch November 3, 2025 22:12
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.

3 participants