fix: serialize nested BaseModel values in dict inputs (v0.5.7)#159
fix: serialize nested BaseModel values in dict inputs (v0.5.7)#159dineshreddy91 wants to merge 1 commit intomainfrom
Conversation
… TypeError When passing dict inputs with nested Pydantic BaseModel values (e.g. MessageContent), the requests library's json.dumps() would fail with "Object of type MessageContent is not JSON serializable". This adds recursive serialization of BaseModel instances within dict inputs via _serialize_value(), ensuring all nested models are converted to plain dicts before HTTP transport. Bump version to 0.5.7. Made-with: Cursor
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical serialization issue where Pydantic Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a TypeError during JSON serialization of nested Pydantic BaseModel instances within dictionary inputs by introducing a recursive _serialize_value helper. The changes are well-tested with new unit tests covering various scenarios. I have a few suggestions to improve the implementation's robustness and maintainability.
| return value.model_dump(exclude_none=True) | ||
| elif isinstance(value, dict): | ||
| return {k: Agent._serialize_value(v) for k, v in value.items()} | ||
| elif isinstance(value, list): |
There was a problem hiding this comment.
The current implementation only handles lists. It would be more robust to also handle other sequence types like tuples, which might also contain BaseModel instances. If a tuple of BaseModels is passed, json.dumps will fail.
| elif isinstance(value, list): | |
| elif isinstance(value, (list, tuple)): |
| class MockClient: | ||
| api_key = "test-key" | ||
| base_url = "https://api.vlm.run/v1" | ||
| timeout = 120.0 | ||
| max_retries = 1 | ||
|
|
There was a problem hiding this comment.
To improve maintainability and reduce code duplication, consider defining the MockClient class once at the TestAgentProcessInputs class level or as a pytest fixture. It is repeated in every test method within this test class (e.g., test_process_inputs_with_dict, test_process_inputs_with_basemodel, etc.), including the newly added tests.
| DeprecationWarning, | ||
| stacklevel=3, | ||
| ) | ||
| return {k: self._serialize_value(v) for k, v in inputs.items()} |
There was a problem hiding this comment.
For consistency with how _serialize_value is called within itself recursively, it would be clearer to call it as a static method using Agent._serialize_value(...) here as well, instead of self._serialize_value(...). This makes it more explicit that it's a static method and doesn't depend on the instance state.
| return {k: self._serialize_value(v) for k, v in inputs.items()} | |
| return {k: Agent._serialize_value(v) for k, v in inputs.items()} |
Summary
TypeError: Object of type MessageContent is not JSON serializablewhen passing dict inputs containing nested PydanticBaseModelvalues (e.g.MessageContent) toclient.agent.execute()._serialize_value()static method that recursively walks dict/list structures and callsmodel_dump(exclude_none=True)on anyBaseModelinstances before the payload reachesjson.dumps().0.5.7.Reproducer (before fix)
Test plan
TestAgentProcessInputs::test_process_inputs_with_dict— plain dict inputs still workTestAgentProcessInputs::test_process_inputs_with_basemodel— BaseModel inputs still workTestAgentProcessInputs::test_process_inputs_with_none— None inputs still worktest_process_inputs_dict_with_nested_basemodel— MessageContent in dict is serializedtest_process_inputs_dict_with_list_of_basemodels— list of MessageContent is serializedtest_process_inputs_dict_plain_values_unchanged— plain values pass through unchangedtest_process_inputs_dict_with_nested_dict_containing_basemodel— deeply nested BaseModel is serializedMade with Cursor