- Notifications
You must be signed in to change notification settings - Fork1k
Open
Labels
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue inhttps://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
Gemini unable to stream structured output. It just return the full structured output directly, not by chunk.
Example Code
frompydantic_ai.providers.google_glaimportGoogleGLAProviderfrompydantic_ai.models.geminiimportGeminiModel,GeminiModelSettingsfrompydanticimportBaseModelmodel=GeminiModel(model_name="gemini-2.0-flash-001",provider=GoogleGLAProvider(api_key=config["GEMINI_API_KEY"]),)classResponse(BaseModel):sentiment:Sentiment=Field(description="The sentiment of the user's message.")response:str=Field(description="Response to the user's message in Traditional Chinese (Taiwan)." )agent=Agent(model,result_type=Response,model_settings=GeminiModelSettings(temperature=0.0),)asyncwithagent.run_stream(user_input,message_history=message_history)asresult:previous_response=""asyncformessage,lastinresult.stream_structured(debounce_by=0.5):try:response=awaitresult.validate_structured_result(message,allow_partial=notlast )print("response",response)# Response(sentiment=Sentiment(sentiment='neutral'), response='...')ifresponse.response:# Only print the new part that wasn't in the previous responsenew_content=response.response[len(previous_response):]ifnew_content:print(new_content,end="",flush=True)previous_response=response.responseexceptExceptionase:continue
Python, Pydantic AI & LLM client version
Python: 3.11.11Pydantic: 2.10.6Pydantic-ai-slim: 0.0.43