diff --git a/src/google/adk/integrations/openai/_openai_llm.py b/src/google/adk/integrations/openai/_openai_llm.py index b2439e06f8d..be4a791f918 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..2bd02fbb178 100644 --- a/tests/unittests/integrations/openai/test_openai_llm.py +++ b/tests/unittests/integrations/openai/test_openai_llm.py @@ -1628,6 +1628,77 @@ 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.OBJECT, + properties={ + "tags": types.Schema( + type=types.Type.ARRAY, + items=types.Schema(type=types.Type.STRING), + ), + }, + ), + { + "type": "object", + "properties": { + "tags": {"type": "array", "items": {"type": "string"}}, + }, + "additionalProperties": False, + "required": ["tags"], + }, + ), + ( + 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."""