Skip to content

Conversation

@Viicos
Copy link
Member

@Viicos Viicos commented Dec 10, 2025

Fixes #3685.

Todo still: update tests in https://github.com/pydantic/pydantic-ai/blob/main/tests/test_function_schema.py to generate the whole function schema to test the actual logic. The issue is that when function_schema() relied on _takes_ctx() previous to this PR, the logic to unwrap the original function differed: _takes_ctx() unwrapped a whole bunch of possible wrappers (properties, classmethods, partial, even classes having a __call__()), which is not the case with _typing_extra.get_function_type_hints() (which only unwrapped partials).

To maintain backwards compatibility, I kept the the partial unwrapping, and will see what needs to be taken into account when updating such tests.

This is a first step in removing usage of this private Pydantic utility, I will then do follow up PRs.

Comment on lines +129 to +120
if index == 0 and takes_ctx is None:
takes_ctx = p.annotation is not sig.empty and _is_call_ctx(type_hints[name])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using _takes_ctx(), do the logic here. This avoids computing the type hints twice.



def _takes_ctx(callable_obj: TargetCallable[P, R]) -> TypeIs[WithCtx[P, R]]:
def _takes_ctx(callable_obj: TargetCallable[P, R]) -> TypeIs[WithCtx[P, R]]: # pyright: ignore[reportUnusedFunction]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still used in one other place in Pydantic AI, will see how this can be cleaned up in a future PR.

@github-actions
Copy link

github-actions bot commented Dec 10, 2025

Docs Preview

commit: 13fd648
Preview URL: https://5a77f943-pydantic-ai-previews.pydantic.workers.dev

@Viicos Viicos force-pushed the vp/fix-function-schema branch from 777fbe7 to 13fd648 Compare December 10, 2025 11:53
@Viicos Viicos marked this pull request as draft December 10, 2025 11:54
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.14: Tool definition not working with sync_to_async

2 participants