Skip to content
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

Anthropic streaming returns in single chunk when agent result_type is specified #823

Open
seunggs opened this issue Jan 31, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@seunggs
Copy link

seunggs commented Jan 31, 2025

I just tried Anthropic streaming (pydantic-ai v0.0.20) and the stream comes in as one big chunk, not as an actual stream (although there is no longer an error about anthropic streaming not being supported). I can confirm the code is correct since a simple switch to "openai:gpt-4o" model creates an actual stream.

...
async with agent.run_stream(
    user_prompt=prompt,
    deps=agent_deps,
    message_history=message_history
) as result:
    # Stream the model's response
    async for chunk in result.stream():
        print("\nStreaming chunk:", chunk)
        yield f"event: document_messages\ndata: {json.dumps(chunk)}\n\n"
...

I discovered that this only happens if I specify the result_type (streaming works fine without):

class DocumentModelResponse(TypedDict, total=False):
    model_response: str
    document_md: str

agent = Agent(
    agent_data["model"],
    deps_type=AgentDeps,
    result_type=DocumentModelResponse,
    tools=tools,
    system_prompt=system_prompt
)
@samuelcolvin
Copy link
Member

My guess is this will be fixed by #582.

@seunggs
Copy link
Author

seunggs commented Jan 31, 2025

@samuelcolvin thanks for the response!

Hmm, I don't have a deep understanding of that discussion but isn't that ticket related to structured outputs? Claude works fine with result_type for non-streaming use cases so why would that ticket resolve the streaming issue?

@sydney-runkle
Copy link
Member

I can confirm that I also experienced this issue with anthropic.

@sydney-runkle sydney-runkle added the bug Something isn't working label Feb 4, 2025
@seunggs
Copy link
Author

seunggs commented Feb 4, 2025

Thanks for confirming @sydney-runkle - let me know if you have an ETA on this. Would really love to get streaming work for my app. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants