Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Gemini unable to stream structured output #1237

Open
@myway840731

Description

@myway840731

Initial Checks

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp