pydantic_ai.result
ResultData
module-attribute
ResultData = TypeVar('ResultData')
Type variable for the result data of a run.
RunResult
dataclass
Bases: _BaseRunResult[ResultData]
Result of a non-streamed run.
See _BaseRunResult
for other available methods.
StreamedRunResult
dataclass
Bases: _BaseRunResult[ResultData]
, Generic[AgentDeps, ResultData]
Result of a streamed run that returns structured data via a tool call.
See _BaseRunResult
for other available methods.
is_complete
class-attribute
instance-attribute
is_complete: bool = False
Whether the stream has all been received.
This is set to True
when one of
stream
,
stream_text
,
stream_structured
or
get_data
completes.
stream
async
stream(
*,
text_delta: bool = False,
debounce_by: float | None = 0.1
) -> AsyncIterator[ResultData]
Stream the response as an async iterable.
Result validators are called on each iteration, if text_delta=False
(the default) or for structured
responses.
Note
Result validators will NOT be called on the text result if text_delta=True
.
The pydantic validator for structured data will be called in partial mode on each iteration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text_delta
|
bool
|
if |
False
|
debounce_by
|
float | None
|
by how much (if at all) to debounce/group the response chunks by. |
0.1
|
Returns:
Type | Description |
---|---|
AsyncIterator[ResultData]
|
An async iterable of the response data. |
stream_text
async
stream_text(
*,
text_delta: bool = False,
debounce_by: float | None = 0.1
) -> AsyncIterator[str]
Stream the text result as an async iterable.
Note
This method will fail if the response is structured,
e.g. if is_structured
returns True
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text_delta
|
bool
|
if |
False
|
debounce_by
|
float | None
|
by how much (if at all) to debounce/group the response chunks by. |
0.1
|
stream_structured
async
stream_structured(
*, debounce_by: float | None = 0.1
) -> AsyncIterator[tuple[ModelStructuredResponse, bool]]
Stream the response as an async iterable of Structured LLM Messages.
Note
This method will fail if the response is text,
e.g. if is_structured
returns False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
debounce_by
|
float | None
|
by how much (if at all) to debounce/group the response chunks by. |
0.1
|
Returns:
Type | Description |
---|---|
AsyncIterator[tuple[ModelStructuredResponse, bool]]
|
An async iterable of the structured response message and whether that is the last message. |
is_structured
property
is_structured: bool
Return whether the stream response contains structured data (as opposed to text).
cost
cost() -> Cost
Return the cost of the whole run.
Note
This won't return the full cost until the stream is finished.
validate_structured_result
async
validate_structured_result(
message: ModelStructuredResponse,
*,
allow_partial: bool = False
) -> ResultData
Validate a structured result message.
_BaseRunResult
dataclass
Bases: ABC
, Generic[ResultData]
Base type for results.
You should not import or use this type directly, instead use its subclasses
RunResult
and StreamedRunResult
instead.
new_messages
Return new messages associated with this run.
System prompts and any messages from older runs are excluded.
Cost
dataclass
Cost of a request or run.
Responsibility for calculating costs is on the model used, PydanticAI simply sums the cost of requests.
You'll need to look up the documentation of the model you're using to convent "token count" costs to monetary costs.
request_tokens
class-attribute
instance-attribute
request_tokens: int | None = None
Tokens used in processing the request.
response_tokens
class-attribute
instance-attribute
response_tokens: int | None = None
Tokens used in generating the response.
total_tokens
class-attribute
instance-attribute
total_tokens: int | None = None
Total tokens used in the whole run, should generally be equal to request_tokens + response_tokens
.
details
class-attribute
instance-attribute
Any extra details returned by the model.