-
Notifications
You must be signed in to change notification settings - Fork 1k
Python: Support pydantic model as function call result #2606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for Pydantic BaseModel subclasses as function call results in the agent framework's type system. Previously, only objects with a to_dict() method were properly serialized; this change enables Pydantic models to be serialized using their native model_dump() method.
Key Changes:
- Added
BaseModelinstance check in_prepare_function_call_results_as_dumpable()before the genericto_dict()fallback - Comprehensive test coverage for single models, lists, dicts, and nested Pydantic models
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/packages/core/agent_framework/_types.py |
Added BaseModel check and model_dump() call in the serialization helper function |
python/packages/core/tests/core/test_types.py |
Added test helper classes and four test functions covering various Pydantic model serialization scenarios |
45ed62c to
671cafa
Compare
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
@copilot there are some code quality checks failing, please fix |
The Python - Code Quality and Python - Tests workflows are now passing. The CI checks completed successfully after the initial run. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Co-authored-by: eavanvalkenburg <[email protected]>
671cafa to
64b46d7
Compare
|
Hello everyone. I'm happy to see, that pydantic models are now supported as function tool output.
The resulting _prepare_function_call_results_as_dumpable(...) would then be: def _prepare_function_call_results_as_dumpable(content: Contents | Any | list[Contents | Any]) -> Any:
if isinstance(content, list):
# Particularly deal with lists of Content
return [_prepare_function_call_results_as_dumpable(item) for item in content]
if isinstance(content, dict):
return {k: _prepare_function_call_results_as_dumpable(v) for k, v in content.items()}
if hasattr(content, "to_dict"):
return content.to_dict(exclude={"raw_representation", "additional_properties"})
if isinstance(content, BaseModel):
return content.model_dump_json()
return contentIf you agree with my points (either one of them or both), I would be glad you would assign the issue to me, so I can make the suggested changes and adapt test cases accordingly. |
Motivation and Context
Fixes #236
Pydantic
BaseModelsubclasses returned from tool functions were not being serialized correctly. The_prepare_function_call_results_as_dumpablefunction checked forto_dict()which doesn't exist on Pydantic models.Description
Added
BaseModelisinstance check before theto_dictfallback, callingmodel_dump()for Pydantic models:_prepare_function_call_results_as_dumpableto detectBaseModelsubclasses and usemodel_dump()Contribution Checklist
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.openai.com/home/REDACTED/work/agent-framework/agent-framework/python/.venv/bin/pytest pytest --cov=agent_framework --cov-report=term-missing:skip-covered tests(dns block)astral.sh/usr/bin/curl curl -LsSf REDACTED(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.