Repository navigation
Fix AFC crash on date, UUID, Enum and list-of-bytes tool returns - #3441
Open
Sun-Wukhan wants to merge 1 commit into
Open
Sun-Wukhan wants to merge 1 commit into
Sun-Wukhan wants to merge 1 commit into
Conversation
This branch has not been deployed
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 #3438
Automatic function calling crashes with
TypeError: Object of type ... is not JSON serializablewhen a tool returns values such as adatetime.date,datetime.time,uuid.UUID, anEnum, or a list ofbytes, for example a pydantic model with date and UUID fields. The tool has already run, but the model never receives the result.The crash is in
encode_unserializable_typesingoogle/genai/_common.py, which prepares function responses forjson.dumps. It only convertedbytesanddatetime.datetimescalars, and its list branch had a missingelif, so a list of bytes was overwritten with the raw bytes after being base64 encoded.This change refactors the function to encode each value recursively: bytes are base64 encoded, datetime/date/time use isoformat, UUIDs use str, Enums encode their underlying value, and dicts, lists and tuples are recursed into element by element.
Tests: added 13 unit tests in
google/genai/tests/common/test_common.pycovering scalar date/time/UUID/Enum/bytes, lists of bytes, mixed and nested lists, lists of UUIDs, and dicts nested in lists. 10 of the new tests fail on the previous code and all pass with this change.google/genai/tests/types/test_bytes_internal.pyalso passes (103 tests passed in total across both files).