Skip to content

Fix AFC crash on date, UUID, Enum and list-of-bytes tool returns - #3441

Open
Sun-Wukhan wants to merge 1 commit into
googleapis:mainfrom
Sun-Wukhan:fix/afc-unserializable-types
Open

Sun-Wukhan wants to merge 1 commit into
googleapis:mainfrom
Sun-Wukhan:fix/afc-unserializable-types

Conversation

@Sun-Wukhan

Copy link
Copy Markdown

Fixes #3438

Automatic function calling crashes with TypeError: Object of type ... is not JSON serializable when a tool returns values such as a datetime.date, datetime.time, uuid.UUID, an Enum, or a list of bytes, 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_types in google/genai/_common.py, which prepares function responses for json.dumps. It only converted bytes and datetime.datetime scalars, and its list branch had a missing elif, 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.py covering 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.py also passes (103 tests passed in total across both files).

This branch has not been deployed

No deployments
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.

Automatic function calling crashes with TypeError when a tool returns a date, UUID, Enum or a list of bytes

1 participant