GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — response.parsed is a plain dict, not the model, when response_schema is Optional[Model] or Model | None · Issue #3437 · googleapis/python-genai Backlog DescriptionEnvironment details
DescriptionWith The same happens for any union where a non-model member comes after a model member, for example This affects Steps to reproduceRuns offline. It uses an from typing import Optional import httpx import pydantic from google import genai from google.genai import types class Recipe(pydantic.BaseModel): name: str minutes: int def handler(request): return httpx.Response(200, json={'candidates': [{ 'content': {'role': 'model', 'parts': [{'text': '{"name": "soup", "minutes": 20}'}]}, 'finishReason': 'STOP'}]}) client = genai.Client( api_key='test-key', http_options=types.HttpOptions( httpx_client=httpx.Client(transport=httpx.MockTransport(handler)) ), ) for schema in [Recipe, Optional[Recipe]]: response = client.models.generate_content( model='gemini-2.5-flash', contents='Give me a recipe', config=types.GenerateContentConfig( response_mime_type='application/json', response_schema=schema ), ) print(schema, '->', repr(response.parsed)) Expected: Actual: Root cause
union_types = typing.get_args(response_schema) for union_type in union_types: if issubclass(union_type, pydantic.BaseModel): ... result.parsed = placeholder.placeholder # Recipe instance else: ... result.parsed = json.loads(result_text) # plain dict The loop handles each union member in turn and assigns Suggested fixValidate once against the whole union when any member is a model, and keep the plain union_types = typing.get_args(response_schema) if any( inspect.isclass(union_type) and issubclass(union_type, pydantic.BaseModel) for union_type in union_types ): # validate json.loads(result_text) against `placeholder: response_schema` # once, as the model branch does today ... else: # unchanged: result.parsed = json.loads(result_text) ... Pydantic already handles A tested fix is ready on branch ActivityMetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |