Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai agents python RunResult

From Leeroopedia
Property Value
Implementation Name RunResult
SDK OpenAI Agents Python
Repository openai-agents-python
Source File src/agents/result.py
Line Range L213-317
Import from agents import RunResult
Type Dataclass

Overview

The RunResult class is the return type of Runner.run(). It is a dataclass that extends RunResultBase and encapsulates the complete outcome of an agent execution: the final output, all generated items, raw model responses, guardrail results, the last agent that ran, and any pending interruptions for human-in-the-loop workflows.

Code Reference

Source Location

Property Value
File src/agents/result.py
Class RunResult
Base Class RunResultBase
Lines 213-317

Signature

@dataclass
class RunResult(RunResultBase):
    _last_agent: Agent[Any]
    max_turns: int = 10
    interruptions: list[ToolApprovalItem] = field(default_factory=list)

    @property
    def last_agent(self) -> Agent[Any]: ...

    def to_state(self) -> RunState[Any]: ...

    # Inherited from RunResultBase:
    # input: str | list[TResponseInputItem]
    # new_items: list[RunItem]
    # raw_responses: list[ModelResponse]
    # final_output: Any
    # input_guardrail_results: list[InputGuardrailResult]
    # output_guardrail_results: list[OutputGuardrailResult]
    # tool_input_guardrail_results: list[ToolInputGuardrailResult]
    # tool_output_guardrail_results: list[ToolOutputGuardrailResult]
    # context_wrapper: RunContextWrapper[Any]
    #
    # Inherited methods:
    # final_output_as(cls, raise_if_incorrect_type=False) -> T
    # to_input_list() -> list[TResponseInputItem]
    # release_agents(release_new_items=True) -> None

Import Statement

from agents import RunResult

I/O Contract

Input

RunResult is not constructed directly by users. It is returned by Runner.run() after agent execution completes.

Properties and Fields

Field/Property Type Description
input list[TResponseInputItem] The original input items provided to the run (may be mutated by handoff input filters).
new_items list[RunItem] All items generated during the run: messages, tool calls, tool outputs, handoff events.
raw_responses list[ModelResponse] The raw LLM responses from each model invocation during the run.
final_output Any The final output of the last agent. Type is str by default, or matches the agent's output_type if specified.
input_guardrail_results list[InputGuardrailResult] Results from input guardrail evaluations.
output_guardrail_results list[OutputGuardrailResult] Results from output guardrail evaluations.
tool_input_guardrail_results list[ToolInputGuardrailResult] Results from tool input guardrail evaluations.
tool_output_guardrail_results list[ToolOutputGuardrailResult] Results from tool output guardrail evaluations.
context_wrapper RunContextWrapper[Any] The context wrapper containing the mutable context object.
max_turns int The maximum number of turns that were allowed for this run (default 10).
interruptions list[ToolApprovalItem] Pending tool approval requests. Non-empty when the run paused for human-in-the-loop approval.
last_agent Agent[Any] Property returning the last agent that was run (important for multi-agent handoff workflows).

Key Methods

Method Return Type Description
final_output_as(cls, raise_if_incorrect_type=False) T Casts final_output to a specific type. Optionally raises TypeError if the type does not match.
to_input_list() list[TResponseInputItem] Merges the original input with all new items into a new input list for multi-turn conversations.
to_state() RunState[Any] Creates a RunState from this result for resuming interrupted runs.
release_agents(release_new_items=True) None Releases strong references to agents for garbage collection after inspection is complete.

Usage Examples

Basic Output Access

from agents import Agent, Runner

agent = Agent(name="assistant", instructions="You are helpful.")
result = await Runner.run(agent, "Hello")
print(result.final_output)       # The text response
print(result.last_agent.name)    # "assistant"
print(len(result.new_items))     # Number of generated items

Typed Output Extraction

from pydantic import BaseModel
from agents import Agent, Runner

class CityInfo(BaseModel):
    name: str
    population: int
    country: str

agent = Agent(
    name="geography",
    instructions="Extract city information.",
    output_type=CityInfo,
)
result = await Runner.run(agent, "Tell me about Paris.")
city = result.final_output_as(CityInfo, raise_if_incorrect_type=True)
print(city.name)        # "Paris"
print(city.population)  # approximate population
print(city.country)     # "France"

Multi-Turn Conversation

result = await Runner.run(agent, "Hello")
print(result.final_output)  # The text response

# Build input for the next turn
next_input = result.to_input_list()
next_input.append({"role": "user", "content": "Follow up question"})
result2 = await Runner.run(agent, next_input)
print(result2.final_output)

Human-in-the-Loop Approval

result = await Runner.run(agent, "Use the delete_file tool")

if result.interruptions:
    # Inspect and approve the pending tool call
    state = result.to_state()
    state.approve(result.interruptions[0])

    # Resume the run from where it was interrupted
    result = await Runner.run(agent, state)
    print(result.final_output)

Inspecting Generated Items

result = await Runner.run(agent, "Research this topic")

for item in result.new_items:
    print(f"Type: {type(item).__name__}")
    print(f"Agent: {item.agent.name if item.agent else 'N/A'}")

# Check guardrail results
for gr in result.input_guardrail_results:
    print(f"Input guardrail: {gr}")
for gr in result.output_guardrail_results:
    print(f"Output guardrail: {gr}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment