From f60cfd032bc4150af85a22dd89baa4d373ca5086 Mon Sep 17 00:00:00 2001 From: Yasser-Ameur Date: Thu, 22 Oct 2026 16:03:00 +0200 Subject: [PATCH 1/2] fix(openai): send the user's types.Schema output_schema to Chat Completions OpenAILlm checked isinstance(schema, BaseModel) before anything else, and types.Schema is a pydantic model, so a types.Schema response_schema was turned into schema.__class__.model_json_schema(): the JSON schema of the Schema class itself. After the strict pass the request carried {"$ref": "#/$defs/Schema"} under the name "Schema" and the user's schema was never enforced. Convert a types.Schema with model_dump and lowercase its types before the BaseModel branch, as OpenAIResponsesLlm._schema_to_dict and LiteLlm already do. --- .../adk/integrations/openai/_openai_llm.py | 7 ++- .../integrations/openai/test_openai_llm.py | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/google/adk/integrations/openai/_openai_llm.py b/src/google/adk/integrations/openai/_openai_llm.py index 31445ce5b80..b2c25ecd762 100644 --- a/src/google/adk/integrations/openai/_openai_llm.py +++ b/src/google/adk/integrations/openai/_openai_llm.py @@ -435,7 +435,12 @@ async def generate_content_async( schema_name = "response" schema_dict = {} - if isinstance(schema, type) and issubclass(schema, BaseModel): + if isinstance(schema, types.Schema): + schema_dict = schema.model_dump( + by_alias=True, exclude_none=True, mode="json" + ) + lowercase_schema_types(schema_dict) + elif isinstance(schema, type) and issubclass(schema, BaseModel): schema_dict = schema.model_json_schema() schema_name = schema.__name__ elif isinstance(schema, BaseModel): diff --git a/tests/unittests/integrations/openai/test_openai_llm.py b/tests/unittests/integrations/openai/test_openai_llm.py index 1badc484925..465c88957bb 100644 --- a/tests/unittests/integrations/openai/test_openai_llm.py +++ b/tests/unittests/integrations/openai/test_openai_llm.py @@ -1628,6 +1628,65 @@ async def test_response_schema_dict_becomes_json_schema_format(): assert "name" in response_format["json_schema"]["schema"]["properties"] +@pytest.mark.asyncio +@pytest.mark.parametrize( + "response_schema, expected_schema", + [ + ( + types.Schema( + type=types.Type.ARRAY, + items=types.Schema(type=types.Type.STRING), + ), + {"type": "array", "items": {"type": "string"}}, + ), + ( + types.Schema( + type=types.Type.OBJECT, + properties={ + "name": types.Schema(type=types.Type.STRING), + "age": types.Schema(type=types.Type.INTEGER), + }, + ), + { + "type": "object", + "properties": { + "name": {"type": "string"}, + "age": {"type": "integer"}, + }, + "additionalProperties": False, + "required": ["age", "name"], + }, + ), + ], +) +async def test_response_schema_genai_schema_becomes_json_schema_format( + response_schema, expected_schema +): + """A types.Schema response_schema is sent as the user's own schema. + + Previously the BaseModel branch caught the types.Schema instance and sent + the JSON schema of the types.Schema class itself. + """ + with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "test_key"}): + openai_llm = OpenAILlm(model="gpt-4o") + llm_request = LlmRequest( + model="gpt-4o", + contents=[Content(role="user", parts=[Part.from_text(text="Hi")])], + config=types.GenerateContentConfig(response_schema=response_schema), + ) + + create_kwargs = await _capture_create_kwargs(openai_llm, llm_request) + + assert create_kwargs["response_format"] == { + "type": "json_schema", + "json_schema": { + "name": "response", + "strict": True, + "schema": expected_schema, + }, + } + + @pytest.mark.asyncio async def test_response_mime_type_json_becomes_json_object_format(): """response_mime_type application/json maps to a json_object format.""" From 4749a165b03c695ca0eec268449c1226cdf9799b Mon Sep 17 00:00:00 2001 From: Yasser-Ameur Date: Sun, 8 Nov 2026 13:01:31 +0100 Subject: [PATCH 2/2] test: cover a types.Schema object with an array property instead of an array root --- .../integrations/openai/test_openai_llm.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/unittests/integrations/openai/test_openai_llm.py b/tests/unittests/integrations/openai/test_openai_llm.py index 465c88957bb..2bd02fbb178 100644 --- a/tests/unittests/integrations/openai/test_openai_llm.py +++ b/tests/unittests/integrations/openai/test_openai_llm.py @@ -1634,10 +1634,22 @@ async def test_response_schema_dict_becomes_json_schema_format(): [ ( types.Schema( - type=types.Type.ARRAY, - items=types.Schema(type=types.Type.STRING), + type=types.Type.OBJECT, + properties={ + "tags": types.Schema( + type=types.Type.ARRAY, + items=types.Schema(type=types.Type.STRING), + ), + }, ), - {"type": "array", "items": {"type": "string"}}, + { + "type": "object", + "properties": { + "tags": {"type": "array", "items": {"type": "string"}}, + }, + "additionalProperties": False, + "required": ["tags"], + }, ), ( types.Schema(