@@ -243,40 +243,54 @@ async def run(
243
243
conversation_id : str | None = None ,
244
244
session : Session | None = None ,
245
245
) -> RunResult :
246
- """Run a workflow starting at the given agent. The agent will run in a loop until a final
247
- output is generated. The loop runs like so:
248
- 1. The agent is invoked with the given input.
249
- 2. If there is a final output (i.e. the agent produces something of type
250
- `agent.output_type`, the loop terminates.
251
- 3. If there's a handoff, we run the loop again, with the new agent.
252
- 4. Else, we run tool calls (if any), and re-run the loop.
246
+ """
247
+ Run a workflow starting at the given agent.
248
+
249
+ The agent will run in a loop until a final output is generated. The loop runs like so:
250
+
251
+ 1. The agent is invoked with the given input.
252
+ 2. If there is a final output (i.e. the agent produces something of type
253
+ `agent.output_type`), the loop terminates.
254
+ 3. If there's a handoff, we run the loop again, with the new agent.
255
+ 4. Else, we run tool calls (if any), and re-run the loop.
256
+
253
257
In two cases, the agent may raise an exception:
254
- 1. If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
255
- 2. If a guardrail tripwire is triggered, a GuardrailTripwireTriggered exception is raised.
256
- Note that only the first agent's input guardrails are run.
258
+
259
+ 1. If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
260
+ 2. If a guardrail tripwire is triggered, a GuardrailTripwireTriggered
261
+ exception is raised.
262
+
263
+ Note:
264
+ Only the first agent's input guardrails are run.
265
+
257
266
Args:
258
267
starting_agent: The starting agent to run.
259
- input: The initial input to the agent. You can pass a single string for a user message,
260
- or a list of input items.
268
+ input: The initial input to the agent. You can pass a single string for a
269
+ user message, or a list of input items.
261
270
context: The context to run the agent with.
262
- max_turns: The maximum number of turns to run the agent for. A turn is defined as one
263
- AI invocation (including any tool calls that might occur).
271
+ max_turns: The maximum number of turns to run the agent for. A turn is
272
+ defined as one AI invocation (including any tool calls that might occur).
264
273
hooks: An object that receives callbacks on various lifecycle events.
265
274
run_config: Global settings for the entire agent run.
266
- previous_response_id: The ID of the previous response, if using OpenAI models via the
267
- Responses API, this allows you to skip passing in input from the previous turn.
268
- conversation_id: The conversation ID (https://platform.openai.com/docs/guides/conversation-state?api-mode=responses).
275
+ previous_response_id: The ID of the previous response. If using OpenAI
276
+ models via the Responses API, this allows you to skip passing in input
277
+ from the previous turn.
278
+ conversation_id: The conversation ID
279
+ (https://platform.openai.com/docs/guides/conversation-state?api-mode=responses).
269
280
If provided, the conversation will be used to read and write items.
270
281
Every agent will have access to the conversation history so far,
271
- and it's output items will be written to the conversation.
282
+ and its output items will be written to the conversation.
272
283
We recommend only using this if you are exclusively using OpenAI models;
273
284
other model providers don't write to the Conversation object,
274
285
so you'll end up having partial conversations stored.
275
286
session: A session for automatic conversation history management.
287
+
276
288
Returns:
277
- A run result containing all the inputs, guardrail results and the output of the last
278
- agent. Agents may perform handoffs, so we don't know the specific type of the output.
289
+ A run result containing all the inputs, guardrail results and the output of
290
+ the last agent. Agents may perform handoffs, so we don't know the specific
291
+ type of the output.
279
292
"""
293
+
280
294
runner = DEFAULT_AGENT_RUNNER
281
295
return await runner .run (
282
296
starting_agent ,
@@ -304,37 +318,52 @@ def run_sync(
304
318
conversation_id : str | None = None ,
305
319
session : Session | None = None ,
306
320
) -> RunResult :
307
- """Run a workflow synchronously, starting at the given agent. Note that this just wraps the
308
- `run` method, so it will not work if there's already an event loop (e.g. inside an async
309
- function, or in a Jupyter notebook or async context like FastAPI). For those cases, use
310
- the `run` method instead.
311
- The agent will run in a loop until a final output is generated. The loop runs like so:
312
- 1. The agent is invoked with the given input.
313
- 2. If there is a final output (i.e. the agent produces something of type
314
- `agent.output_type`, the loop terminates.
315
- 3. If there's a handoff, we run the loop again, with the new agent.
316
- 4. Else, we run tool calls (if any), and re-run the loop.
321
+ """
322
+ Run a workflow synchronously, starting at the given agent.
323
+
324
+ Note:
325
+ This just wraps the `run` method, so it will not work if there's already an
326
+ event loop (e.g. inside an async function, or in a Jupyter notebook or async
327
+ context like FastAPI). For those cases, use the `run` method instead.
328
+
329
+ The agent will run in a loop until a final output is generated. The loop runs:
330
+
331
+ 1. The agent is invoked with the given input.
332
+ 2. If there is a final output (i.e. the agent produces something of type
333
+ `agent.output_type`), the loop terminates.
334
+ 3. If there's a handoff, we run the loop again, with the new agent.
335
+ 4. Else, we run tool calls (if any), and re-run the loop.
336
+
317
337
In two cases, the agent may raise an exception:
318
- 1. If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
319
- 2. If a guardrail tripwire is triggered, a GuardrailTripwireTriggered exception is raised.
320
- Note that only the first agent's input guardrails are run.
338
+
339
+ 1. If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
340
+ 2. If a guardrail tripwire is triggered, a GuardrailTripwireTriggered
341
+ exception is raised.
342
+
343
+ Note:
344
+ Only the first agent's input guardrails are run.
345
+
321
346
Args:
322
347
starting_agent: The starting agent to run.
323
- input: The initial input to the agent. You can pass a single string for a user message,
324
- or a list of input items.
348
+ input: The initial input to the agent. You can pass a single string for a
349
+ user message, or a list of input items.
325
350
context: The context to run the agent with.
326
- max_turns: The maximum number of turns to run the agent for. A turn is defined as one
327
- AI invocation (including any tool calls that might occur).
351
+ max_turns: The maximum number of turns to run the agent for. A turn is
352
+ defined as one AI invocation (including any tool calls that might occur).
328
353
hooks: An object that receives callbacks on various lifecycle events.
329
354
run_config: Global settings for the entire agent run.
330
- previous_response_id: The ID of the previous response, if using OpenAI models via the
331
- Responses API, this allows you to skip passing in input from the previous turn.
355
+ previous_response_id: The ID of the previous response, if using OpenAI
356
+ models via the Responses API, this allows you to skip passing in input
357
+ from the previous turn.
332
358
conversation_id: The ID of the stored conversation, if any.
333
359
session: A session for automatic conversation history management.
360
+
334
361
Returns:
335
- A run result containing all the inputs, guardrail results and the output of the last
336
- agent. Agents may perform handoffs, so we don't know the specific type of the output.
362
+ A run result containing all the inputs, guardrail results and the output of
363
+ the last agent. Agents may perform handoffs, so we don't know the specific
364
+ type of the output.
337
365
"""
366
+
338
367
runner = DEFAULT_AGENT_RUNNER
339
368
return runner .run_sync (
340
369
starting_agent ,
@@ -361,34 +390,49 @@ def run_streamed(
361
390
conversation_id : str | None = None ,
362
391
session : Session | None = None ,
363
392
) -> RunResultStreaming :
364
- """Run a workflow starting at the given agent in streaming mode. The returned result object
365
- contains a method you can use to stream semantic events as they are generated.
393
+ """
394
+ Run a workflow starting at the given agent in streaming mode.
395
+
396
+ The returned result object contains a method you can use to stream semantic
397
+ events as they are generated.
398
+
366
399
The agent will run in a loop until a final output is generated. The loop runs like so:
367
- 1. The agent is invoked with the given input.
368
- 2. If there is a final output (i.e. the agent produces something of type
369
- `agent.output_type`, the loop terminates.
370
- 3. If there's a handoff, we run the loop again, with the new agent.
371
- 4. Else, we run tool calls (if any), and re-run the loop.
400
+
401
+ 1. The agent is invoked with the given input.
402
+ 2. If there is a final output (i.e. the agent produces something of type
403
+ `agent.output_type`), the loop terminates.
404
+ 3. If there's a handoff, we run the loop again, with the new agent.
405
+ 4. Else, we run tool calls (if any), and re-run the loop.
406
+
372
407
In two cases, the agent may raise an exception:
373
- 1. If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
374
- 2. If a guardrail tripwire is triggered, a GuardrailTripwireTriggered exception is raised.
375
- Note that only the first agent's input guardrails are run.
408
+
409
+ 1. If the max_turns is exceeded, a MaxTurnsExceeded exception is raised.
410
+ 2. If a guardrail tripwire is triggered, a GuardrailTripwireTriggered
411
+ exception is raised.
412
+
413
+ Note:
414
+ Only the first agent's input guardrails are run.
415
+
376
416
Args:
377
417
starting_agent: The starting agent to run.
378
- input: The initial input to the agent. You can pass a single string for a user message,
379
- or a list of input items.
418
+ input: The initial input to the agent. You can pass a single string for a
419
+ user message, or a list of input items.
380
420
context: The context to run the agent with.
381
- max_turns: The maximum number of turns to run the agent for. A turn is defined as one
382
- AI invocation (including any tool calls that might occur).
421
+ max_turns: The maximum number of turns to run the agent for. A turn is
422
+ defined as one AI invocation (including any tool calls that might occur).
383
423
hooks: An object that receives callbacks on various lifecycle events.
384
424
run_config: Global settings for the entire agent run.
385
- previous_response_id: The ID of the previous response, if using OpenAI models via the
386
- Responses API, this allows you to skip passing in input from the previous turn.
425
+ previous_response_id: The ID of the previous response, if using OpenAI
426
+ models via the Responses API, this allows you to skip passing in input
427
+ from the previous turn.
387
428
conversation_id: The ID of the stored conversation, if any.
388
429
session: A session for automatic conversation history management.
430
+
389
431
Returns:
390
- A result object that contains data about the run, as well as a method to stream events.
432
+ A result object that contains data about the run, as well as a method to
433
+ stream events.
391
434
"""
435
+
392
436
runner = DEFAULT_AGENT_RUNNER
393
437
return runner .run_streamed (
394
438
starting_agent ,
0 commit comments