Skip to content

Add new _GeminiThoughtPart to adhere to Gemini response #1897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025

Conversation

dnouri
Copy link
Contributor

@dnouri dnouri commented Jun 3, 2025

The VertexAI docs mention that fields thought and thoughSignature might be present in a part. The thought field indicates "if the part is thought from the model". See
https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/Content#Part

This is a simple fix to avoid validation errors because the current schema refuses to accept responses that contain a 'thought part' currently.

The failure would look like this:

File "/var/lang/lib/python3.12/site-packages/pydantic/type_adapter.py", line 468, in validate_json
  return self.validator.validate_json(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for _GeminiResponse
candidates.0.content.parts.0.text.text
Field required [type=missing, input_value={'thought': True, 'though...CPmF0DeNlHMlqQaW2asg9c'}, input_type=dict]
  For further information visit https://errors.pydantic.dev/2.11/v/missing

@dnouri dnouri force-pushed the fix-gemini-thought-part branch 2 times, most recently from d302160 to f2a2e05 Compare June 3, 2025 13:33
The VertexAI docs mention that fields `thought` and `thoughSignature`
might be present in a part.  The `thought` field indicates "if the
part is thought from the model".  See
https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/Content#Part

This is a simple fix to avoid validation errors because the current
schema refuses to accept responses that contain a 'thought part'
currently.

The failure would look like this:

```
File "/var/lang/lib/python3.12/site-packages/pydantic/type_adapter.py", line 468, in validate_json
  return self.validator.validate_json(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for _GeminiResponse
candidates.0.content.parts.0.text.text
Field required [type=missing, input_value={'thought': True, 'though...CPmF0DeNlHMlqQaW2asg9c'}, input_type=dict]
  For further information visit https://errors.pydantic.dev/2.11/v/missing
```
@dnouri dnouri force-pushed the fix-gemini-thought-part branch from f2a2e05 to 4bc6db7 Compare June 3, 2025 13:37
@DouweM DouweM merged commit a7b8074 into pydantic:main Jun 3, 2025
18 checks passed
@DouweM
Copy link
Contributor

DouweM commented Jun 3, 2025

@dnouri Thanks Daniel, I've released a new version: https://github.com/pydantic/pydantic-ai/releases/tag/v0.2.13

@dnouri dnouri deleted the fix-gemini-thought-part branch June 3, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BaseModel output type no longer working for Gemini models
3 participants