-
Notifications
You must be signed in to change notification settings - Fork 817
Structured Output fails with text output + Behaviour inconsistency #1590
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
Comments
@IngLP Can you please change |
Tried it, it doesn't work. Removing str from output_type PREVENTS the LLM to output text. |
Moreover, this also blocks you from using stream_text(), which I need to show reasoning progress to the user. |
@IngLP Thanks for trying that, you're right that that would not be the desired result... To help us debug this further, can you please port your code to the new |
@IngLP Also, have you considered making |
@DouweM indeed, this is exactly the workaround I have set up now. But this is not elegant at all, since it is a NOT supported approach (see issue #1189 ) and makes you abuse the deps. |
@IngLP Did you try the new That works as expected with import asyncio
from typing import Union
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.messages import (
PartDeltaEvent,
PartStartEvent,
TextPart,
TextPartDelta,
)
class CallAgent(BaseModel):
agent_name: str
agent = Agent[None, CallAgent](
model="google-gla:gemini-2.5-flash-preview-04-17",
output_type=Union[str, CallAgent], # pyright: ignore
instructions="Say hello and then transfer the user to 'user_assistant' agent",
)
async def test_with_iter():
async with agent.iter(user_prompt="Hello") as run:
async for node in run:
if Agent.is_model_request_node(node):
async with node.stream(run.ctx) as request_stream:
async for event in request_stream:
if isinstance(event, PartStartEvent) and isinstance(event.part, TextPart):
print(event.part.content, end="", flush=True)
elif isinstance(event, PartDeltaEvent) and isinstance(event.delta, TextPartDelta):
print(event.delta.content_delta, end="", flush=True)
assert run.result.output == CallAgent(agent_name="user_assistant")
asyncio.run(test_with_iter()) |
This works for this simple case, but it doesn't do all the processing and handling performed by agent.run_stream() |
@IngLP What specific behavior are you missing? |
I mean, I would have to re-implement all the logic from here:
|
@IngLP A good amount of that is already covered by the |
Initial Checks
Description
Situation:
This is NOT working in streaming mode, but it does in NON-streaming mode.
See below tests. The first fails. The second passes.
Example Code
Python, Pydantic AI & LLM client version
The text was updated successfully, but these errors were encountered: