From 0c503ad898e7dae9458b5112746fc3b5dcbaf8ea Mon Sep 17 00:00:00 2001 From: zhang wenkai <81221238+zwk110@users.noreply.github.com> Date: Tue, 20 Oct 2026 22:39:33 +0800 Subject: [PATCH] fix: serialize GenAI response schema instances Handle GenAI Schema values without passing instances to TypeAdapter. Preserve JSON aliases, normalize nested types, and cover human-input request events. --- src/google/adk/utils/_schema_utils.py | 6 +++ tests/unittests/utils/test_schema_utils.py | 44 +++++++++++++++++++ .../utils/test_workflow_hitl_utils.py | 23 ++++++++++ 3 files changed, 73 insertions(+) diff --git a/src/google/adk/utils/_schema_utils.py b/src/google/adk/utils/_schema_utils.py index 432ac5fcece..5a85d0eb3fd 100644 --- a/src/google/adk/utils/_schema_utils.py +++ b/src/google/adk/utils/_schema_utils.py @@ -127,6 +127,12 @@ def schema_to_json_schema(schema: SchemaType) -> dict[str, Any]: """ if isinstance(schema, dict): return schema + if isinstance(schema, types.Schema): + json_schema = schema.model_dump( + mode="json", by_alias=True, exclude_none=True + ) + lowercase_schema_types(json_schema) + return json_schema return TypeAdapter(schema).json_schema() diff --git a/tests/unittests/utils/test_schema_utils.py b/tests/unittests/utils/test_schema_utils.py index 719c404eb2c..e3a1a4b449b 100644 --- a/tests/unittests/utils/test_schema_utils.py +++ b/tests/unittests/utils/test_schema_utils.py @@ -442,6 +442,50 @@ def test_raw_string_not_parsed_with_str_schema(self): class TestSchemaToJsonSchema: """Tests for schema_to_json_schema function.""" + @pytest.mark.parametrize( + "schema_type, expected_type", + [ + (types.Type.STRING, "string"), + (types.Type.INTEGER, "integer"), + (types.Type.BOOLEAN, "boolean"), + ], + ) + def test_genai_schema_instance_is_serialized( + self, schema_type: types.Type, expected_type: str + ) -> None: + """A GenAI schema instance describes its value, not its own model fields.""" + schema = types.Schema(type=schema_type) + + assert schema_to_json_schema(schema) == {"type": expected_type} + + def test_nested_genai_schema_preserves_aliases_and_input(self) -> None: + """Nested types use JSON Schema names without modifying the input schema.""" + schema = types.Schema( + type=types.Type.OBJECT, + properties={ + "tags": types.Schema( + type=types.Type.ARRAY, + items=types.Schema(type=types.Type.STRING, min_length=1), + ), + }, + required=["tags"], + ) + original = schema.model_dump() + + result = schema_to_json_schema(schema) + + assert result == { + "type": "object", + "properties": { + "tags": { + "type": "array", + "items": {"type": "string", "minLength": 1}, + }, + }, + "required": ["tags"], + } + assert schema.model_dump() == original + def test_dict_schema_is_returned_unchanged(self): """A raw dict is already JSON Schema, so it must not be re-derived.""" raw = {"type": "object", "properties": {"name": {"type": "string"}}} diff --git a/tests/unittests/workflow/utils/test_workflow_hitl_utils.py b/tests/unittests/workflow/utils/test_workflow_hitl_utils.py index 01f64ab2344..96b0e0f3020 100644 --- a/tests/unittests/workflow/utils/test_workflow_hitl_utils.py +++ b/tests/unittests/workflow/utils/test_workflow_hitl_utils.py @@ -110,6 +110,29 @@ def test_create_confirmation_request_input(): class TestCreateRequestInputEvent: + def test_with_genai_response_schema(self) -> None: + """A GenAI response schema is included in the human-input event.""" + request = RequestInput( + interrupt_id="genai-schema", + response_schema=types.Schema( + type=types.Type.OBJECT, + properties={"approved": types.Schema(type=types.Type.BOOLEAN)}, + required=["approved"], + ), + ) + + event = create_request_input_event(request) + + assert event.content is not None + call = event.content.parts[0].function_call + assert call.args["response_schema"] == { + "type": "object", + "properties": {"approved": {"type": "boolean"}}, + "required": ["approved"], + } + assert event.long_running_tool_ids == {"genai-schema"} + assert event.model_dump_json() + def test_basic_event(self): ri = RequestInput( interrupt_id="test-id",