Repository navigation
fix(core): serialize nested BaseModel instances in to_json_attr - #273
Closed
pcbeingused33 wants to merge 1 commit into
Closed
pcbeingused33 wants to merge 1 commit into
pcbeingused33 wants to merge 1 commit into
Conversation
to_json_attr only dumped a top-level BaseModel; a list or dict holding models made json.dumps raise TypeError and fell back to str(), producing invalid JSON in the input/output span attributes. Pass a default handler that dumps nested models with model_dump(mode='json', by_alias=True, exclude_none=True), matching _encode_json_value in genkit-otel. Fixes genkit-ai#80
|
Thanks for your interest in Genkit. We're closing this pull request because we can't accept it in its current form. If you'd like to contribute, please start a discussion with the maintainers on an issue first; see CONTRIBUTING.md. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #80
Cause.
to_json_attronly handled aBaseModelat the top level. When an action or flow takes or returns a container holding models (e.g.list[Reservation],dict[str, Reservation]),json.dumpsraisedTypeErrorand the function fell back tostr(value), so the input/output span attributes held a Python repr instead of valid JSON.Fix. Pass a
defaulthandler tojson.dumpsthat dumpsBaseModelinstances viamodel_dump(mode='json', by_alias=True, exclude_none=True), matching_encode_json_valueingenkit-otel. Anything else still raisesTypeError, so the existingstr()fallback is unchanged.Test. Added tests in
instrumentation_test.py: nested models in a list and a dict round-trip throughjson.loads(with aliases andNonefields dropped), and an unserializable object still falls back tostr(). The first fails on main and passes with the fix.This PR was prepared with AI assistance (Claude Code); the change and its tests were reviewed before submission.