module documentation

Workflow-specific primitives for working with the OpenAI Agents SDK in a workflow context

Exception ToolSerializationError Error that occurs when a tool output could not be serialized.
Function activity_as_tool Convert a single Temporal activity function to an OpenAI agent tool.
Function nexus_operation_as_tool Convert a Nexus operation into an OpenAI agent tool.
Function stateful_mcp_server A stateful MCP server implementation for Temporal workflows.
Function stateless_mcp_server A stateless MCP server implementation for Temporal workflows.
def activity_as_tool(fn: Callable, *, task_queue: str | None = None, schedule_to_close_timeout: timedelta | None = None, schedule_to_start_timeout: timedelta | None = None, start_to_close_timeout: timedelta | None = None, heartbeat_timeout: timedelta | None = None, retry_policy: RetryPolicy | None = None, cancellation_type: ActivityCancellationType = ActivityCancellationType.TRY_CANCEL, activity_id: str | None = None, versioning_intent: VersioningIntent | None = None, summary: str | None = None, priority: Priority = Priority.default, strict_json_schema: bool = True) -> Tool: (source)

Convert a single Temporal activity function to an OpenAI agent tool.

Warning

This API is experimental and may change in future versions. Use with caution in production environments.

This function takes a Temporal activity function and converts it into an OpenAI agent tool that can be used by the agent to execute the activity during workflow execution. The tool will automatically handle the conversion of inputs and outputs between the agent and the activity. Note that if you take a context, mutation will not be persisted, as the activity may not be running in the same location.

Example

>>> @activity.defn
>>> def process_data(input: str) -> str:
...     return f"Processed: {input}"
>>>
>>> # Create tool with custom activity options
>>> tool = activity_as_tool(
...     process_data,
...     start_to_close_timeout=timedelta(seconds=30),
...     retry_policy=RetryPolicy(maximum_attempts=3),
...     heartbeat_timeout=timedelta(seconds=10)
... )
>>> # Use tool with an OpenAI agent
Parameters
fn:CallableA Temporal activity function to convert to a tool.
task_queue:str | NoneUndocumented
schedule_to_close_timeout:timedelta | NoneUndocumented
schedule_to_start_timeout:timedelta | NoneUndocumented
start_to_close_timeout:timedelta | NoneUndocumented
heartbeat_timeout:timedelta | NoneUndocumented
retry_policy:RetryPolicy | NoneUndocumented
cancellation_type:ActivityCancellationTypeUndocumented
activity_id:str | NoneUndocumented
versioning_intent:VersioningIntent | NoneUndocumented
summary:str | NoneUndocumented
priority:PriorityUndocumented
strict_json_schema:boolWhether the tool should follow a strict schema. See https://openai.github.io/openai-agents-python/ref/tool/#agents.tool.FunctionTool.strict_json_schema
For other arguments
refer to workflow start_activity
Returns
ToolAn OpenAI agent tool that wraps the provided activity.
Raises
ApplicationErrorIf the function is not properly decorated as a Temporal activity.
def nexus_operation_as_tool(operation: nexusrpc.Operation[Any, Any], *, service: type[Any], endpoint: str, schedule_to_close_timeout: timedelta | None = None, strict_json_schema: bool = True) -> Tool: (source)

Convert a Nexus operation into an OpenAI agent tool.

Warning

This API is experimental and may change in future versions. Use with caution in production environments.

This function takes a Nexus operation and converts it into an OpenAI agent tool that can be used by the agent to execute the operation during workflow execution. The tool will automatically handle the conversion of inputs and outputs between the agent and the operation.

Example

>>> @nexusrpc.service
... class WeatherService:
...     get_weather_object_nexus_operation: nexusrpc.Operation[WeatherInput, Weather]
>>>
>>> # Create tool with custom activity options
>>> tool = nexus_operation_as_tool(
...     WeatherService.get_weather_object_nexus_operation,
...     service=WeatherService,
...     endpoint="weather-service",
... )
>>> # Use tool with an OpenAI agent
Parameters
operation:nexusrpc.Operation[Any, Any]Undocumented
service:type[Any]The Nexus service class that contains the operation.
endpoint:strThe Nexus endpoint to use for the operation.
schedule_to_close_timeout:timedelta | NoneUndocumented
strict_json_schema:boolWhether the tool should follow a strict schema
fnA Nexus operation to convert into a tool.
Returns
ToolAn OpenAI agent tool that wraps the provided operation.
def stateful_mcp_server(name: str, config: ActivityConfig | None = None, server_session_config: ActivityConfig | None = None) -> AbstractAsyncContextManager[MCPServer]: (source)

A stateful MCP server implementation for Temporal workflows.

Warning

This API is experimental and may change in future versions. Use with caution in production environments.

This wraps an MCP server to maintain a persistent connection throughout the workflow execution. It creates a dedicated worker that stays connected to the MCP server and processes operations on a dedicated task queue.

This approach is more efficient for workflows that make multiple MCP calls, as it avoids connection overhead, but requires more resources to maintain the persistent connection and worker.

The caller will have to handle cases where the dedicated worker fails, as Temporal is unable to seamlessly recreate any lost state in that case.

Parameters
name:strA string name for the server. Should match that provided in the plugin.
config:ActivityConfig | NoneOptional activity configuration for MCP operation activities. Defaults to 1-minute start-to-close and 30-second schedule-to-start timeouts.
server_session_config:ActivityConfig | NoneOptional activity configuration for the connection activity. Defaults to 1-hour start-to-close timeout.
Returns
AbstractAsyncContextManager[MCPServer]Undocumented
def stateless_mcp_server(name: str, config: ActivityConfig | None = None, cache_tools_list: bool = False) -> MCPServer: (source)

A stateless MCP server implementation for Temporal workflows.

Warning

This API is experimental and may change in future versions. Use with caution in production environments.

This uses a TemporalMCPServer of the same name registered with the OpenAIAgents plugin to implement durable MCP operations statelessly.

This approach is suitable for simple use cases where connection overhead is acceptable and you don't need to maintain state between operations. It should be preferred to stateful when possible due to its superior durability guarantees.

Parameters
name:strA string name for the server. Should match that provided in the plugin.
config:ActivityConfig | NoneOptional activity configuration for MCP operation activities. Defaults to 1-minute start-to-close timeout.
cache_tools_list:boolIf true, the list of tools will be cached for the duration of the server
Returns
MCPServerUndocumented