Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/google/adk/utils/_schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
44 changes: 44 additions & 0 deletions tests/unittests/utils/test_schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}}
Expand Down
23 changes: 23 additions & 0 deletions tests/unittests/workflow/utils/test_workflow_hitl_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading