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

Commitcefe6a3

Browse files
authored
MakeAgentStream.stream_output (available insideagent.iter) stream validated output data instead of raising validation errors (#2134)
1 parentd10f036 commitcefe6a3

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

‎examples/pydantic_ai_examples/stream_whales.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
fromtypingimportAnnotated
1212

1313
importlogfire
14-
frompydanticimportField,ValidationError
14+
frompydanticimportField
1515
fromrich.consoleimportConsole
1616
fromrich.liveimportLive
1717
fromrich.tableimportTable
@@ -51,20 +51,7 @@ async def main():
5151
)asresult:
5252
console.print('Response:',style='green')
5353

54-
asyncformessage,lastinresult.stream_structured(debounce_by=0.01):
55-
try:
56-
whales=awaitresult.validate_structured_output(
57-
message,allow_partial=notlast
58-
)
59-
exceptValidationErrorasexc:
60-
ifall(
61-
e['type']=='missing'ande['loc']== ('response',)
62-
foreinexc.errors()
63-
):
64-
continue
65-
else:
66-
raise
67-
54+
asyncforwhalesinresult.stream(debounce_by=0.01):
6855
table=Table(
6956
title='Species of Whale',
7057
caption='Streaming Structured responses from GPT-4',

‎pydantic_ai_slim/pydantic_ai/result.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ async def stream_output(self, *, debounce_by: float | None = 0.1) -> AsyncIterat
5959
"""Asynchronously stream the (validated) agent outputs."""
6060
asyncforresponseinself.stream_responses(debounce_by=debounce_by):
6161
ifself._final_result_eventisnotNone:
62-
yieldawaitself._validate_response(response,self._final_result_event.tool_name,allow_partial=True)
62+
try:
63+
yieldawaitself._validate_response(
64+
response,self._final_result_event.tool_name,allow_partial=True
65+
)
66+
exceptValidationError:
67+
pass
6368
ifself._final_result_eventisnotNone:# pragma: no branch
6469
yieldawaitself._validate_response(
6570
self._raw_stream_response.get(),self._final_result_event.tool_name,allow_partial=False

‎tests/test_streaming.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ def call_final_result_with_bad_data(messages: list[ModelMessage], info: AgentInf
10561056
)
10571057

10581058

1059-
asyncdeftest_stream_prompted_output():
1059+
asyncdeftest_stream_structured_output():
10601060
classCityLocation(BaseModel):
10611061
city:str
10621062
country:str|None=None
@@ -1079,6 +1079,30 @@ class CityLocation(BaseModel):
10791079
assertresult.is_complete
10801080

10811081

1082+
asyncdeftest_iter_stream_structured_output():
1083+
classCityLocation(BaseModel):
1084+
city:str
1085+
country:str|None=None
1086+
1087+
m=TestModel(custom_output_text='{"city": "Mexico City", "country": "Mexico"}')
1088+
1089+
agent=Agent(m,output_type=PromptedOutput(CityLocation))
1090+
1091+
asyncwithagent.iter('')asrun:
1092+
asyncfornodeinrun:
1093+
ifagent.is_model_request_node(node):
1094+
asyncwithnode.stream(run.ctx)asstream:
1095+
assert [casyncforcinstream.stream_output(debounce_by=None)]==snapshot(
1096+
[
1097+
CityLocation(city='Mexico '),
1098+
CityLocation(city='Mexico City'),
1099+
CityLocation(city='Mexico City'),
1100+
CityLocation(city='Mexico City',country='Mexico'),
1101+
CityLocation(city='Mexico City',country='Mexico'),
1102+
]
1103+
)
1104+
1105+
10821106
deftest_function_tool_event_tool_call_id_properties():
10831107
"""Ensure that the `tool_call_id` property on function tool events mirrors the underlying part's ID."""
10841108
# Prepare a ToolCallPart with a fixed ID

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp