Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai agents python OpenAIResponsesModel Get Response

From Leeroopedia
Property Value
Implementation Name OpenAIResponsesModel Get Response
SDK OpenAI Agents Python
Repository openai-agents-python
Source File src/agents/models/openai_responses.py
Line Range L82-157
Import from agents.models.openai_responses import OpenAIResponsesModel
Type Async instance method (internal API)

Overview

The OpenAIResponsesModel.get_response() method is the non-streaming model invocation implementation that uses the OpenAI Responses API. It is called internally by the Runner during each turn of the execution loop. This method assembles the request parameters, calls the OpenAI API, processes the response into a standardized ModelResponse, and integrates with the tracing system for observability.

Note: This is an internal API. Users typically do not call this method directly; the Runner handles model invocation automatically.

Code Reference

Source Location

Property Value
File src/agents/models/openai_responses.py
Class OpenAIResponsesModel
Method get_response
Lines 82-157

Signature

async def get_response(
    self,
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchemaBase | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
    previous_response_id: str | None = None,
    conversation_id: str | None = None,
    prompt: ResponsePromptParam | None = None,
) -> ModelResponse:

Import Statement

from agents.models.openai_responses import OpenAIResponsesModel

I/O Contract

Inputs

Parameter Type Default Description
system_instructions None (required) The system prompt derived from the agent's instructions. None if the agent has no instructions.
input list[TResponseInputItem] (required) The conversation history or a simple string user message.
model_settings ModelSettings (required) Model tuning parameters (temperature, top_p, max_tokens, etc.).
tools list[Tool] (required) Tool definitions to make available to the model for function calling.
output_schema None (required) JSON schema constraining the structured output format, or None for free-form text.
handoffs list[Handoff] (required) Handoff definitions exposed to the model as transfer tools.
tracing ModelTracing (required) Tracing configuration controlling whether input/output data is recorded in spans.
previous_response_id None None ID of the previous response for server-managed conversation continuity.
conversation_id None None Server-side conversation identifier.
prompt None None A prompt parameter for the Responses API prompt feature.

Output

Type Description
ModelResponse Contains output (list of response output items), usage (token counts and details), and response_id (unique response identifier).

Internal Execution Flow

The get_response() method performs the following steps:

  1. Opens a tracing span: A response_span is created (unless tracing is disabled) to capture the model call for observability.
  2. Calls _fetch_response(): Delegates to the internal method that constructs the full API request parameters and calls the OpenAI Responses API with stream=False.
  3. Logs the response: If debug logging is enabled and data logging is not suppressed, the response output items are logged as formatted JSON.
  4. Extracts usage statistics: Converts the API response's usage object into the SDK's Usage dataclass with token count breakdowns.
  5. Records tracing data: If tracing is configured to include data, the response and input are attached to the span.
  6. Handles errors: If an exception occurs, it is recorded in the tracing span with error details and re-raised. For APIStatusError exceptions, the request ID is included in the log.
  7. Returns ModelResponse: Constructs and returns a ModelResponse with the output items, usage statistics, and response ID.

Usage Examples

Since this is an internal API, direct usage is uncommon. However, it can be useful for testing or custom model implementations:

Understanding the Internal Call

# This is what the Runner does internally on each turn:
from agents.models.openai_responses import OpenAIResponsesModel
from agents.models.interface import ModelTracing

model = OpenAIResponsesModel(model="gpt-4.1", openai_client=client)

response = await model.get_response(
    system_instructions="You are a helpful assistant.",
    input="What is 2 + 2?",
    model_settings=ModelSettings(temperature=0.0),
    tools=[],
    output_schema=None,
    handoffs=[],
    tracing=ModelTracing.ENABLED,
)

print(response.output)       # List of response output items
print(response.usage)        # Token usage statistics
print(response.response_id)  # Unique response identifier

ModelResponse Structure

# The ModelResponse returned contains:
# - output: list of response output items (text, tool calls, etc.)
# - usage: Usage(requests=1, input_tokens=N, output_tokens=M, total_tokens=N+M)
# - response_id: str identifier for conversation continuity

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment