Structured-output continuation merge raises AttributeError: __pydantic_generic_metadata__ when a hop has parsed JSON
Maintainer thường phản hồi trong vòng 1 ngày
@Venkaiahbabuneelam đang làm issue này rồi.
Từ ngày 11/11/2026.
- #3351 của @copybara-service — đang mở
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Summary
Structured-output generation can fail inside merge_continuation_responses() when a continuation hop already has a non-null parsed dictionary. The merge serializes that derived field back into GenerateContentResponse._from_response(). Extra-field pruning then treats the dictionary as the bare pydantic.BaseModel and calls BaseModel.model_rebuild(), raising:
AttributeError: __pydantic_generic_metadata__
We originally encountered this SDK stack during an asynchronous Vertex AI generate_content() call with response_json_schema. The deterministic reproduction below uses synthetic responses only: no credentials, model access, network requests, or application data are required.
Environment details
- Programming language: Python
- OS for local reproduction: macOS (Darwin)
- Language runtime version: Python 3.12.13
- Package versions:
google-genai==3.1.0,pydantic==2.13.5 - Also reproduced against unmodified
mainatf1670003f5eaf9d367ab414b8c5d757c991d8cc0with the same Python/Pydantic versions.
Steps to reproduce
-
Install the released packages in an isolated Python 3.12 environment:
python -m pip install "google-genai==3.1.0" "pydantic==2.13.5" -
Run the following script. It models a thought-only continuation hop followed by a complete JSON answer. The final hop's
parsedvalue is produced by the SDK's own_from_response(), not assigned manually.
import platform
import traceback
import pydantic
from google import genai
from google.genai import _extra_utils, types
print(f"Python={platform.python_version()} OS={platform.system()}")
print(f"google-genai={genai.__version__} pydantic={pydantic.__version__}")
schema = {
"type": "object",
"properties": {"name": {"type": "string"}},
"required": ["name"],
}
config = {"response_mime_type": "application/json", "response_json_schema": schema}
# A thought-only continuation hop, followed by a complete JSON response.
first = types.GenerateContentResponse(
candidates=[types.Candidate(
content=types.Content(role="model", parts=[
types.Part(text="Thinking about the answer.", thought=True)
]),
finish_reason="CONTINUATION",
)]
)
last = types.GenerateContentResponse._from_response(
response={"candidates": [{
"content": {"role": "model", "parts": [{"text": '{"name":"Example"}'}]},
"finish_reason": "STOP",
}]},
kwargs={"config": config},
)
print("last.parsed:", last.parsed)
assert last.parsed == {"name": "Example"}
print("one hop:", _extra_utils.merge_continuation_responses([last], config=config).parsed)
try:
merged = _extra_utils.merge_continuation_responses([first, last], config=config)
print("two hops:", merged.parsed)
except Exception:
traceback.print_exc()
raise SystemExit(1)
Expected behavior
Both the single-hop control and the two-hop merge return parsed == {"name": "Example"}. The derived parsed field should be recomputed from the merged answer without being treated as an API response field.
Actual behavior
The single-hop control succeeds; the two-hop merge raises the exception above and exits with status 1:
Python=3.12.13 OS=Darwin
google-genai=3.1.0 pydantic=2.13.5
last.parsed: {'name': 'Example'}
one hop: {'name': 'Example'}
Relevant stack frames from the local reproduction (environment-specific path prefixes omitted):
google/genai/_extra_utils.py:1338, in merge_continuation_responses
reparsed = types.GenerateContentResponse._from_response(...)
google/genai/types.py:9000, in _from_response
result = super()._from_response(response=response, kwargs=kwargs)
google/genai/_common.py:688, in _from_response
_remove_extra_fields(cls, response)
google/genai/_common.py:393, in _remove_extra_fields
_remove_extra_fields(model_type, value)
google/genai/_common.py:365, in _remove_extra_fields
model.model_rebuild(raise_errors=False)
pydantic/main.py:683, in model_rebuild
return _model_construction.complete_model_class(...)
pydantic/_internal/_model_construction.py:632, in complete_model_class
typevars_map = get_model_typevars_map(cls)
pydantic/_internal/_generics.py:236, in get_model_typevars_map
generic_metadata = cls.__pydantic_generic_metadata__
AttributeError: __pydantic_generic_metadata__
Analysis and possible fix
merge_continuation_responses()retains a hop'sparsedvalue and calls_from_response(response=merged.model_dump(exclude_none=True), ...)to recompute structured output.GenerateContentResponse.parsedis annotated asOptional[Union[pydantic.BaseModel, dict[Any, Any], Enum]]._remove_extra_fields()takes the first union member, then recursively visits the actual dictionary aspydantic.BaseModel. Itsmodel_rebuild()call on that base class causes this exception.
A narrowly scoped candidate is to exclude the derived parsed field from the serialization used for reparsing:
reparsed = types.GenerateContentResponse._from_response(
- response=merged.model_dump(exclude_none=True),
+ response=merged.model_dump(exclude_none=True, exclude={'parsed'}),
kwargs={'config': config_dict},
)
I verified that applying only this change to the checkout above makes the same reproduction return two hops: {'name': 'Example'}. This is a candidate fix, not a claim that the full SDK test suite has been validated.
A regression test should cover a non-null parsed dictionary on a hop, including the thought-only-continuation/final-JSON case above. The existing structured continuation test starts with two partial JSON responses whose parsed values are unset, so it does not exercise this case.
- Ngôn ngữ chính
- Python
- Star
- 4.1k
- Fork
- 1.1k
- Merge trung bình
- 1 ngày 20 giờ
- Pull request đã merge (30 ngày)
- 90
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của googleapis/python-genai
-
Interactions retries ignore the documented `initial_delay` and `max_delay` defaultsCó thể đã có người làm @Venkaiahbabuneelam đã nhận 4 ngày trước. Đang mởpriority: p2 status:awaiting user response type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
googleapis/python-genai#3317 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Test import module that doesn't existCó thể đã có người làm @Venkaiahbabuneelam đã nhận 30 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
googleapis/python-genai#3122 · 6 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Curated history keeps half a user turn when the model turn is invalidCó thể đã có người làm @Venkaiahbabuneelam đã nhận 39 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
googleapis/python-genai#3051 · 3 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: Unsubscripted typing.List and typing.Dict crash convert_if_exist_pydantic_model in AFCCó thể đã có người làm @Venkaiahbabuneelam đã nhận 39 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
googleapis/python-genai#3044 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Automatic function calling rejects `datetime` and `UUID` argumentsCó thể đã có người làm @Venkaiahbabuneelam đã nhận 3 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 75/100
googleapis/python-genai#3331 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của googleapis/python-genai
Issue tương tự
-
Contributing instructionsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
dbt-labs/spark-utils#51 ·
-
bug pydanty:is-working
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
pydantic/pydantic-ai#13444 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
good first issue help wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
-
changelog investigate
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
ramnes/notion-sdk-py#502 ·