Runner
Runner
Source code in src/agents/run.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
run
async
classmethod
run(
starting_agent: Agent[TContext],
input: str | list[TResponseInputItem],
*,
context: TContext | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
hooks: RunHooks[TContext] | None = None,
run_config: RunConfig | None = None,
previous_response_id: str | None = None,
conversation_id: str | None = None,
session: Session | None = None,
) -> RunResult
Run a workflow starting at the given agent.
The agent will run in a loop until a final output is generated. The loop runs like so:
- The agent is invoked with the given input.
- If there is a final output (i.e. the agent produces something of type
agent.output_type
), the loop terminates. - If there's a handoff, we run the loop again, with the new agent.
- Else, we run tool calls (if any), and re-run the loop.
In two cases, the agent may raise an exception:
- If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
- If a guardrail tripwire is triggered, a GuardrailTripwireTriggered exception is raised.
Note
Only the first agent's input guardrails are run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starting_agent
|
Agent[TContext]
|
The starting agent to run. |
required |
input
|
str | list[TResponseInputItem]
|
The initial input to the agent. You can pass a single string for a user message, or a list of input items. |
required |
context
|
TContext | None
|
The context to run the agent with. |
None
|
max_turns
|
int
|
The maximum number of turns to run the agent for. A turn is defined as one AI invocation (including any tool calls that might occur). |
DEFAULT_MAX_TURNS
|
hooks
|
RunHooks[TContext] | None
|
An object that receives callbacks on various lifecycle events. |
None
|
run_config
|
RunConfig | None
|
Global settings for the entire agent run. |
None
|
previous_response_id
|
str | None
|
The ID of the previous response. If using OpenAI models via the Responses API, this allows you to skip passing in input from the previous turn. |
None
|
conversation_id
|
str | None
|
The conversation ID (https://platform.openai.com/docs/guides/conversation-state?api-mode=responses). If provided, the conversation will be used to read and write items. Every agent will have access to the conversation history so far, and its output items will be written to the conversation. We recommend only using this if you are exclusively using OpenAI models; other model providers don't write to the Conversation object, so you'll end up having partial conversations stored. |
None
|
session
|
Session | None
|
A session for automatic conversation history management. |
None
|
Returns:
Type | Description |
---|---|
RunResult
|
A run result containing all the inputs, guardrail results and the output of |
RunResult
|
the last agent. Agents may perform handoffs, so we don't know the specific |
RunResult
|
type of the output. |
Source code in src/agents/run.py
run_sync
classmethod
run_sync(
starting_agent: Agent[TContext],
input: str | list[TResponseInputItem],
*,
context: TContext | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
hooks: RunHooks[TContext] | None = None,
run_config: RunConfig | None = None,
previous_response_id: str | None = None,
conversation_id: str | None = None,
session: Session | None = None,
) -> RunResult
Run a workflow synchronously, starting at the given agent.
Note
This just wraps the run
method, so it will not work if there's already an
event loop (e.g. inside an async function, or in a Jupyter notebook or async
context like FastAPI). For those cases, use the run
method instead.
The agent will run in a loop until a final output is generated. The loop runs:
- The agent is invoked with the given input.
- If there is a final output (i.e. the agent produces something of type
agent.output_type
), the loop terminates. - If there's a handoff, we run the loop again, with the new agent.
- Else, we run tool calls (if any), and re-run the loop.
In two cases, the agent may raise an exception:
- If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
- If a guardrail tripwire is triggered, a GuardrailTripwireTriggered exception is raised.
Note
Only the first agent's input guardrails are run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starting_agent
|
Agent[TContext]
|
The starting agent to run. |
required |
input
|
str | list[TResponseInputItem]
|
The initial input to the agent. You can pass a single string for a user message, or a list of input items. |
required |
context
|
TContext | None
|
The context to run the agent with. |
None
|
max_turns
|
int
|
The maximum number of turns to run the agent for. A turn is defined as one AI invocation (including any tool calls that might occur). |
DEFAULT_MAX_TURNS
|
hooks
|
RunHooks[TContext] | None
|
An object that receives callbacks on various lifecycle events. |
None
|
run_config
|
RunConfig | None
|
Global settings for the entire agent run. |
None
|
previous_response_id
|
str | None
|
The ID of the previous response, if using OpenAI models via the Responses API, this allows you to skip passing in input from the previous turn. |
None
|
conversation_id
|
str | None
|
The ID of the stored conversation, if any. |
None
|
session
|
Session | None
|
A session for automatic conversation history management. |
None
|
Returns:
Type | Description |
---|---|
RunResult
|
A run result containing all the inputs, guardrail results and the output of |
RunResult
|
the last agent. Agents may perform handoffs, so we don't know the specific |
RunResult
|
type of the output. |
Source code in src/agents/run.py
run_streamed
classmethod
run_streamed(
starting_agent: Agent[TContext],
input: str | list[TResponseInputItem],
context: TContext | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
hooks: RunHooks[TContext] | None = None,
run_config: RunConfig | None = None,
previous_response_id: str | None = None,
conversation_id: str | None = None,
session: Session | None = None,
) -> RunResultStreaming
Run a workflow starting at the given agent in streaming mode.
The returned result object contains a method you can use to stream semantic events as they are generated.
The agent will run in a loop until a final output is generated. The loop runs like so:
- The agent is invoked with the given input.
- If there is a final output (i.e. the agent produces something of type
agent.output_type
), the loop terminates. - If there's a handoff, we run the loop again, with the new agent.
- Else, we run tool calls (if any), and re-run the loop.
In two cases, the agent may raise an exception:
- If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
- If a guardrail tripwire is triggered, a GuardrailTripwireTriggered exception is raised.
Note
Only the first agent's input guardrails are run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starting_agent
|
Agent[TContext]
|
The starting agent to run. |
required |
input
|
str | list[TResponseInputItem]
|
The initial input to the agent. You can pass a single string for a user message, or a list of input items. |
required |
context
|
TContext | None
|
The context to run the agent with. |
None
|
max_turns
|
int
|
The maximum number of turns to run the agent for. A turn is defined as one AI invocation (including any tool calls that might occur). |
DEFAULT_MAX_TURNS
|
hooks
|
RunHooks[TContext] | None
|
An object that receives callbacks on various lifecycle events. |
None
|
run_config
|
RunConfig | None
|
Global settings for the entire agent run. |
None
|
previous_response_id
|
str | None
|
The ID of the previous response, if using OpenAI models via the Responses API, this allows you to skip passing in input from the previous turn. |
None
|
conversation_id
|
str | None
|
The ID of the stored conversation, if any. |
None
|
session
|
Session | None
|
A session for automatic conversation history management. |
None
|
Returns:
Type | Description |
---|---|
RunResultStreaming
|
A result object that contains data about the run, as well as a method to |
RunResultStreaming
|
stream events. |
Source code in src/agents/run.py
RunConfig
dataclass
Configures settings for the entire agent run.
Source code in src/agents/run.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
model
class-attribute
instance-attribute
model: str | Model | None = None
The model to use for the entire agent run. If set, will override the model set on every agent. The model_provider passed in below must be able to resolve this model name.
model_provider
class-attribute
instance-attribute
model_provider: ModelProvider = field(
default_factory=MultiProvider
)
The model provider to use when looking up string model names. Defaults to OpenAI.
model_settings
class-attribute
instance-attribute
model_settings: ModelSettings | None = None
Configure global model settings. Any non-null values will override the agent-specific model settings.
handoff_input_filter
class-attribute
instance-attribute
handoff_input_filter: HandoffInputFilter | None = None
A global input filter to apply to all handoffs. If Handoff.input_filter
is set, then that
will take precedence. The input filter allows you to edit the inputs that are sent to the new
agent. See the documentation in Handoff.input_filter
for more details.
input_guardrails
class-attribute
instance-attribute
input_guardrails: list[InputGuardrail[Any]] | None = None
A list of input guardrails to run on the initial run input.
output_guardrails
class-attribute
instance-attribute
output_guardrails: list[OutputGuardrail[Any]] | None = None
A list of output guardrails to run on the final output of the run.
tracing_disabled
class-attribute
instance-attribute
Whether tracing is disabled for the agent run. If disabled, we will not trace the agent run.
trace_include_sensitive_data
class-attribute
instance-attribute
Whether we include potentially sensitive data (for example: inputs/outputs of tool calls or LLM generations) in traces. If False, we'll still create spans for these events, but the sensitive data will not be included.
workflow_name
class-attribute
instance-attribute
The name of the run, used for tracing. Should be a logical name for the run, like "Code generation workflow" or "Customer support agent".
trace_id
class-attribute
instance-attribute
A custom trace ID to use for tracing. If not provided, we will generate a new trace ID.
group_id
class-attribute
instance-attribute
A grouping identifier to use for tracing, to link multiple traces from the same conversation or process. For example, you might use a chat thread ID.
trace_metadata
class-attribute
instance-attribute
An optional dictionary of additional metadata to include with the trace.
session_input_callback
class-attribute
instance-attribute
Defines how to handle session history when new input is provided.
- None
(default): The new input is appended to the session history.
- SessionInputCallback
: A custom function that receives the history and new input, and
returns the desired combined list of items.
call_model_input_filter
class-attribute
instance-attribute
Optional callback that is invoked immediately before calling the model. It receives the current
agent, context and the model input (instructions and input items), and must return a possibly
modified ModelInputData
to use for the model call.
This allows you to edit the input sent to the model e.g. to stay within a token limit. For example, you can use this to add a system prompt to the input.