Skip to content

Commit 610893e

Browse files
docs: improve core components documentation
Core Components: - Enhanced agent.py base classes documentation - Added comprehensive Computer interface docs - Improved guardrail system documentation Model & Provider: - Enhanced model interfaces documentation - Improved provider management docs - Added chat completion utility documentation MCP: - Added module documentation - Improved server implementation docs Memory: - Enhanced OpenAI conversation session docs Realtime: - Improved realtime agent and event system docs Voice: - Enhanced TTS model settings documentation
1 parent efa88f7 commit 610893e

File tree

13 files changed

+256
-33
lines changed

13 files changed

+256
-33
lines changed

src/agents/agent.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737

3838
@dataclass
3939
class ToolsToFinalOutputResult:
40+
"""Result type for processing tool outputs into final agent outputs.
41+
42+
This class helps manage the transition between tool execution results
43+
and the final output of an agent run. It determines whether more LLM
44+
processing is needed or if we have reached the final output state.
45+
46+
Attributes:
47+
is_final_output: Whether this is the final output. If False, the LLM
48+
will run again and receive the tool call output.
49+
final_output: The final output value. Can be None if `is_final_output`
50+
is False, otherwise must match the `output_type` of the agent.
51+
"""
52+
4053
is_final_output: bool
4154
"""Whether this is the final output. If False, the LLM will run again and receive the tool call
4255
output.
@@ -73,18 +86,31 @@ class MCPConfig(TypedDict):
7386

7487
@dataclass
7588
class AgentBase(Generic[TContext]):
76-
"""Base class for `Agent` and `RealtimeAgent`."""
89+
"""Base class for all agent implementations in the OpenAI Agents SDK.
90+
91+
This class provides the core functionality shared between standard agents
92+
and realtime agents. It manages tools, model settings, and agent configuration.
93+
94+
Generic Args:
95+
TContext: The type of context maintained during agent execution.
96+
97+
Key Features:
98+
- Tool management and execution
99+
- Model configuration
100+
- Handoff support for agent collaboration
101+
- Context management across runs
102+
"""
77103

78104
name: str
79-
"""The name of the agent."""
105+
"""The name of the agent, used for identification and logging."""
80106

81107
handoff_description: str | None = None
82108
"""A description of the agent. This is used when the agent is used as a handoff, so that an
83109
LLM knows what it does and when to invoke it.
84110
"""
85111

86112
tools: list[Tool] = field(default_factory=list)
87-
"""A list of tools that the agent can use."""
113+
"""A list of tools that the agent has access to and can use during execution."""
88114

89115
mcp_servers: list[MCPServer] = field(default_factory=list)
90116
"""A list of [Model Context Protocol](https://modelcontextprotocol.io/) servers that

src/agents/computer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66

77

88
class Computer(abc.ABC):
9-
"""A computer implemented with sync operations. The Computer interface abstracts the
10-
operations needed to control a computer or browser."""
9+
"""Abstract interface for computer and browser control operations.
10+
11+
This interface defines the standard operations that can be performed on a computer
12+
or browser environment, such as:
13+
- Mouse movements and clicks
14+
- Keyboard input
15+
- Screenshot capture
16+
- Window/viewport dimensions
17+
- Environment detection
18+
19+
Implementations should provide synchronous operations for each method to ensure
20+
reliable control across different platforms and environments.
21+
"""
1122

1223
@property
1324
@abc.abstractmethod

src/agents/guardrail.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818

1919
@dataclass
2020
class GuardrailFunctionOutput:
21-
"""The output of a guardrail function."""
21+
"""Output from a guardrail function's validation check.
22+
23+
This class represents the result of executing a guardrail's validation logic.
24+
It includes both the validation result and optional detailed information about
25+
what was checked and why the validation succeeded or failed.
26+
27+
Use this to:
28+
- Track guardrail validation results
29+
- Provide detailed feedback about validation checks
30+
- Control agent execution flow based on validation
31+
"""
2232

2333
output_info: Any
2434
"""
@@ -29,6 +39,7 @@ class GuardrailFunctionOutput:
2939
tripwire_triggered: bool
3040
"""
3141
Whether the tripwire was triggered. If triggered, the agent's execution will be halted.
42+
Set to True to stop agent execution when validation fails.
3243
"""
3344

3445

src/agents/mcp/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""Model Context Protocol (MCP) for OpenAI Agents SDK.
2+
3+
Provides server implementations and utilities for Model Context Protocol,
4+
enabling standardized communication between agents and external tools.
5+
"""
6+
17
try:
28
from .server import (
39
MCPServer,

src/agents/mcp/server.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,17 @@ def create_streams(
230230
GetSessionIdCallback | None,
231231
]
232232
]:
233-
"""Create the streams for the server."""
233+
"""Create communication streams for the MCP server.
234+
235+
Returns:
236+
A context manager that yields a tuple containing:
237+
- A receive stream for incoming messages and exceptions
238+
- A send stream for outgoing messages
239+
- An optional callback for getting session IDs
240+
241+
This method is used internally by server implementations to establish
242+
bidirectional communication channels with the MCP service.
243+
"""
234244
pass
235245

236246
async def __aenter__(self):
@@ -245,6 +255,17 @@ def invalidate_tools_cache(self):
245255
self._cache_dirty = True
246256

247257
async def _run_with_retries(self, func: Callable[[], Awaitable[T]]) -> T:
258+
"""Execute a function with exponential backoff retry logic.
259+
260+
Args:
261+
func: Async function to execute with retries.
262+
263+
Returns:
264+
The result of the function if successful.
265+
266+
Retries failed operations using exponential backoff based on
267+
max_retry_attempts and retry_backoff_seconds_base settings.
268+
"""
248269
attempts = 0
249270
while True:
250271
try:

src/agents/memory/openai_conversations_session.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,33 @@ async def start_openai_conversations_session(openai_client: AsyncOpenAI | None =
2020

2121

2222
class OpenAIConversationsSession(SessionABC):
23+
"""Session implementation using OpenAI's Conversations API for persistence.
24+
25+
This class provides conversation history storage and retrieval using OpenAI's
26+
Conversations API. It automatically manages conversation IDs and handles
27+
API communication for storing and retrieving conversation items.
28+
29+
Features:
30+
- Automatic session creation and management
31+
- Persistent storage via OpenAI's infrastructure
32+
- Support for retrieving paginated history
33+
- Automatic client configuration
34+
"""
35+
2336
def __init__(
2437
self,
2538
*,
2639
conversation_id: str | None = None,
2740
openai_client: AsyncOpenAI | None = None,
2841
):
42+
"""Initialize an OpenAI Conversations session.
43+
44+
Args:
45+
conversation_id: Optional existing conversation ID to use.
46+
If None, a new conversation will be created.
47+
openai_client: Optional custom OpenAI client to use.
48+
If None, uses default client or creates a new one.
49+
"""
2950
self._session_id: str | None = conversation_id
3051
_openai_client = openai_client
3152
if _openai_client is None:

src/agents/models/chatcmpl_helpers.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@
1010

1111

1212
class ChatCmplHelpers:
13+
"""Helper utilities for OpenAI chat completions API integration.
14+
15+
This class provides utilities for working with OpenAI's chat completions API,
16+
handling common tasks like:
17+
- Determining if a client is using OpenAI's official API
18+
- Managing response storage settings
19+
- Configuring streaming options
20+
"""
21+
1322
@classmethod
14-
def is_openai(cls, client: AsyncOpenAI):
23+
def is_openai(cls, client: AsyncOpenAI) -> bool:
24+
"""Check if the client is using the official OpenAI API.
25+
26+
Args:
27+
client: The AsyncOpenAI client instance to check
28+
29+
Returns:
30+
True if using api.openai.com, False otherwise
31+
"""
1532
return str(client.base_url).startswith("https://api.openai.com")
1633

1734
@classmethod

src/agents/models/interface.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,53 @@
1717

1818

1919
class ModelTracing(enum.Enum):
20+
"""Configuration for model execution tracing and debugging.
21+
22+
This enum controls how much information is collected during model execution
23+
for debugging, monitoring, and analysis purposes.
24+
"""
25+
2026
DISABLED = 0
21-
"""Tracing is disabled entirely."""
27+
"""Tracing is disabled entirely. No debug information is collected."""
2228

2329
ENABLED = 1
24-
"""Tracing is enabled, and all data is included."""
30+
"""Full tracing is enabled. All data including inputs and outputs is collected."""
2531

2632
ENABLED_WITHOUT_DATA = 2
27-
"""Tracing is enabled, but inputs/outputs are not included."""
33+
"""Tracing is enabled but sensitive data is excluded. Useful for production monitoring."""
2834

2935
def is_disabled(self) -> bool:
36+
"""Check if tracing is completely disabled.
37+
38+
Returns:
39+
True if no tracing information should be collected.
40+
"""
3041
return self == ModelTracing.DISABLED
3142

3243
def include_data(self) -> bool:
44+
"""Check if full data should be included in traces.
45+
46+
Returns:
47+
True if input/output data should be included in traces.
48+
"""
3349
return self == ModelTracing.ENABLED
3450

3551

3652
class Model(abc.ABC):
37-
"""The base interface for calling an LLM."""
53+
"""Base interface for Large Language Model interactions.
54+
55+
This abstract class defines the contract for all model implementations
56+
in the OpenAI Agents SDK. It provides methods for both standard and
57+
streaming responses from language models.
58+
59+
Key responsibilities:
60+
- Handle model API communication
61+
- Process system instructions and user inputs
62+
- Apply model settings and configurations
63+
- Manage tool and handoff integrations
64+
- Support tracing and debugging
65+
- Handle both sync and streaming responses
66+
"""
3867

3968
@abc.abstractmethod
4069
async def get_response(

src/agents/models/multi_provider.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,37 @@
88

99

1010
class MultiProviderMap:
11-
"""A map of model name prefixes to ModelProviders."""
11+
"""Registry for managing multiple model providers in the system.
12+
13+
This class maintains a mapping between model name prefixes and their
14+
corresponding ModelProvider implementations. It enables:
15+
- Dynamic registration of model providers
16+
- Provider lookup by model prefix
17+
- Support for multiple model backends
18+
- Flexible provider management
19+
20+
Example:
21+
```python
22+
map = MultiProviderMap()
23+
map.add_provider("openai", OpenAIProvider())
24+
map.add_provider("custom", CustomProvider())
25+
provider = map.get_provider("openai") # Get OpenAI provider
26+
```
27+
"""
1228

1329
def __init__(self):
30+
"""Initialize an empty provider mapping."""
1431
self._mapping: dict[str, ModelProvider] = {}
1532

1633
def has_prefix(self, prefix: str) -> bool:
17-
"""Returns True if the given prefix is in the mapping."""
34+
"""Check if a provider exists for the given prefix.
35+
36+
Args:
37+
prefix: The model name prefix to check
38+
39+
Returns:
40+
True if a provider is registered for this prefix
41+
"""
1842
return prefix in self._mapping
1943

2044
def get_mapping(self) -> dict[str, ModelProvider]:

src/agents/realtime/agent.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@
2525

2626
@dataclass
2727
class RealtimeAgent(AgentBase, Generic[TContext]):
28-
"""A specialized agent instance that is meant to be used within a `RealtimeSession` to build
29-
voice agents. Due to the nature of this agent, some configuration options are not supported
30-
that are supported by regular `Agent` instances. For example:
31-
- `model` choice is not supported, as all RealtimeAgents will be handled by the same model
32-
within a `RealtimeSession`.
33-
- `modelSettings` is not supported, as all RealtimeAgents will be handled by the same model
34-
within a `RealtimeSession`.
35-
- `outputType` is not supported, as RealtimeAgents do not support structured outputs.
36-
- `toolUseBehavior` is not supported, as all RealtimeAgents will be handled by the same model
37-
within a `RealtimeSession`.
38-
- `voice` can be configured on an `Agent` level; however, it cannot be changed after the first
39-
agent within a `RealtimeSession` has spoken.
28+
"""Specialized agent for real-time interactive scenarios like voice conversations.
29+
30+
RealtimeAgent is designed to operate within a RealtimeSession for building
31+
interactive voice and streaming agents. It provides real-time response
32+
capabilities while maintaining consistency across a session.
33+
34+
Key Features:
35+
- Real-time streaming responses
36+
- Voice interaction support
37+
- Session-wide model consistency
38+
- Streamlined configuration for real-time use
39+
40+
Limitations:
41+
- Model selection is fixed per RealtimeSession
42+
- Model settings are session-wide
43+
- No structured output support
44+
- Tool behavior is session-controlled
45+
- Voice settings are immutable after first use
46+
47+
Note:
48+
Unlike standard Agents, RealtimeAgents share core settings within
49+
their session to ensure consistent behavior and performance in
50+
real-time scenarios.
4051
4152
See `AgentBase` for base parameters that are shared with `Agent`s.
4253
"""

0 commit comments

Comments
 (0)