Skip to content

Commit f1f88f8

Browse files
authored
fix(result): support Pydantic model_rebuild for RunResultStreaming (#2455)
1 parent 7220437 commit f1f88f8

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/agents/result.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from dataclasses import InitVar, dataclass, field
99
from typing import Any, Literal, TypeVar, cast
1010

11+
from pydantic import GetCoreSchemaHandler
12+
from pydantic_core import core_schema
13+
1114
from .agent import Agent
1215
from .agent_output import AgentOutputSchemaBase
1316
from .exceptions import (
@@ -124,6 +127,16 @@ class RunResultBase(abc.ABC):
124127
_trace_state: TraceState | None = field(default=None, init=False, repr=False)
125128
"""Serialized trace metadata captured during the run."""
126129

130+
@classmethod
131+
def __get_pydantic_core_schema__(
132+
cls,
133+
_source_type: Any,
134+
_handler: GetCoreSchemaHandler,
135+
) -> core_schema.CoreSchema:
136+
# RunResult objects are runtime values; schema generation should treat them as instances
137+
# instead of recursively traversing internal dataclass annotations.
138+
return core_schema.is_instance_schema(cls)
139+
127140
@property
128141
@abc.abstractmethod
129142
def last_agent(self) -> Agent[Any]:

tests/test_result_cast.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99
from openai.types.responses import ResponseOutputMessage, ResponseOutputText
10-
from pydantic import BaseModel
10+
from pydantic import BaseModel, ConfigDict
1111

1212
from agents import (
1313
Agent,
@@ -45,6 +45,16 @@ class Foo(BaseModel):
4545
bar: int
4646

4747

48+
def test_run_result_streaming_supports_pydantic_model_rebuild() -> None:
49+
class StreamingRunContainer(BaseModel):
50+
query_id: str
51+
run_stream: RunResultStreaming | None
52+
53+
model_config = ConfigDict(arbitrary_types_allowed=True)
54+
55+
StreamingRunContainer.model_rebuild()
56+
57+
4858
def _create_message(text: str) -> ResponseOutputMessage:
4959
return ResponseOutputMessage(
5060
id="msg",

0 commit comments

Comments
 (0)