generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

Support for required fields in Pydantic structured-output

Open ravwojdyla opened this issue 1 year ago • 2 comments

Description of the bug:

It appears that all fields in the structured-output are effectively marked as optional by this line: https://github.com/google-gemini/generative-ai-python/blob/b8772ed1424a080911151b354764d76a0e7af2af/google/generativeai/types/content_types.py#L490

It was added in https://github.com/google-gemini/generative-ai-python/commit/e09e7f242abcabe1bda28168be58a751ccdc5c03, possible cause:

service fails if 'required' is used in nested objects

Actual vs expected behavior:

I have a Pydantic schema with nested objects (most of them required). Gemini API consistently doesn't fill all the required fields, which triggers Pydantic validation error. If I monkey-patch the fix or comment out the line https://github.com/google-gemini/generative-ai-python/blob/b8772ed1424a080911151b354764d76a0e7af2af/google/generativeai/types/content_types.py#L490, the Gemini API consistently returns complete JSON objects.

Any other information you'd like to share?

No response

ravwojdyla avatar Nov 29 '24 06:11 ravwojdyla

Close

Aungyehtut8 avatar Nov 29 '24 11:11 Aungyehtut8

Description of the bug:

It appears that all fields in the structured-output are effectively marked as optional by this line:

https://github.com/google-gemini/generative-ai-python/blob/b8772ed1424a080911151b354764d76a0e7af2af/google/generativeai/types/content_types.py#L490

It was added in e09e7f2, possible cause:

service fails if 'required' is used in nested objects

Actual vs expected behavior:

I have a Pydantic schema with nested objects (most of the required). Gemini API consistently doesn't fill all the required fields, which triggers Pydantic validation error. If I monkey-patch the fix by commenting out the line

https://github.com/google-gemini/generative-ai-python/blob/b8772ed1424a080911151b354764d76a0e7af2af/google/generativeai/types/content_types.py#L490

, the Gemini API consistently returns complete json objects.

Any other information you'd like to share?

No response

Ke

Aungyehtut8 avatar Nov 29 '24 11:11 Aungyehtut8