Skip to content

Commit bc949c3

Browse files
docs: reformat run module docstrings to Google style for better mkdocstrings rendering (#1740)
1 parent 85d7d5d commit bc949c3

File tree

1 file changed

+102
-58
lines changed

1 file changed

+102
-58
lines changed

src/agents/run.py

Lines changed: 102 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -243,40 +243,54 @@ async def run(
243243
conversation_id: str | None = None,
244244
session: Session | None = None,
245245
) -> 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+
253257
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+
257266
Args:
258267
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.
261270
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).
264273
hooks: An object that receives callbacks on various lifecycle events.
265274
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).
269280
If provided, the conversation will be used to read and write items.
270281
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.
272283
We recommend only using this if you are exclusively using OpenAI models;
273284
other model providers don't write to the Conversation object,
274285
so you'll end up having partial conversations stored.
275286
session: A session for automatic conversation history management.
287+
276288
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.
279292
"""
293+
280294
runner = DEFAULT_AGENT_RUNNER
281295
return await runner.run(
282296
starting_agent,
@@ -304,37 +318,52 @@ def run_sync(
304318
conversation_id: str | None = None,
305319
session: Session | None = None,
306320
) -> 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+
317337
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+
321346
Args:
322347
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.
325350
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).
328353
hooks: An object that receives callbacks on various lifecycle events.
329354
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.
332358
conversation_id: The ID of the stored conversation, if any.
333359
session: A session for automatic conversation history management.
360+
334361
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.
337365
"""
366+
338367
runner = DEFAULT_AGENT_RUNNER
339368
return runner.run_sync(
340369
starting_agent,
@@ -361,34 +390,49 @@ def run_streamed(
361390
conversation_id: str | None = None,
362391
session: Session | None = None,
363392
) -> 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+
366399
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+
372407
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+
376416
Args:
377417
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.
380420
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).
383423
hooks: An object that receives callbacks on various lifecycle events.
384424
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.
387428
conversation_id: The ID of the stored conversation, if any.
388429
session: A session for automatic conversation history management.
430+
389431
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.
391434
"""
435+
392436
runner = DEFAULT_AGENT_RUNNER
393437
return runner.run_streamed(
394438
starting_agent,

0 commit comments

Comments
 (0)