Implementation:Microsoft Agent framework OpenAIResponsesClient Init
| Knowledge Sources | |
|---|---|
| Domains | AI_Infrastructure, Agent_Architecture |
| Last Updated | 2026-02-11 17:00 GMT |
Overview
Concrete tool for configuring an OpenAI Responses API client provided by the Microsoft Agent Framework.
Description
The OpenAIResponsesClient class initializes an authenticated connection to the OpenAI Responses API. It extends multiple mixin layers providing middleware support, function invocation handling, and telemetry. The client implements the SupportsChatGetResponse protocol, making it compatible with the framework's Agent class and orchestration patterns.
Usage
Import this class when you need to connect to OpenAI's API directly (not through Azure). Configure with an API key (via parameter or environment variable OPENAI_API_KEY) and optionally specify a model ID. Use this client as the foundation for all agent operations.
Code Reference
Source Location
- Repository: agent-framework
- File: python/packages/core/agent_framework/openai/_responses_client.py
- Lines: L1734-1846
Signature
class OpenAIResponsesClient(
OpenAIConfigMixin,
ChatMiddlewareLayer[OpenAIResponsesOptionsT],
FunctionInvocationLayer[OpenAIResponsesOptionsT],
ChatTelemetryLayer[OpenAIResponsesOptionsT],
RawOpenAIResponsesClient[OpenAIResponsesOptionsT],
Generic[OpenAIResponsesOptionsT],
):
def __init__(
self,
*,
model_id: str | None = None,
api_key: str | Callable[[], str | Awaitable[str]] | None = None,
org_id: str | None = None,
base_url: str | None = None,
default_headers: Mapping[str, str] | None = None,
async_client: AsyncOpenAI | None = None,
instruction_role: str | None = None,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
middleware: Sequence[ChatMiddleware | ChatMiddlewareCallable | FunctionMiddleware | FunctionMiddlewareCallable] | None = None,
function_invocation_configuration: FunctionInvocationConfiguration | None = None,
**kwargs: Any,
) -> None:
Import
from agent_framework.openai import OpenAIResponsesClient
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_id | str | No | OpenAI model name (e.g., "gpt-4o"). Also via env var OPENAI_RESPONSES_MODEL_ID. |
| api_key | str or Callable | No | API key or async provider. Also via env var OPENAI_API_KEY. |
| org_id | str | No | Organization ID. Also via env var OPENAI_ORG_ID. |
| base_url | str | No | Custom base URL. Also via env var OPENAI_BASE_URL. |
| default_headers | Mapping[str, str] | No | Default HTTP headers for requests. |
| async_client | AsyncOpenAI | No | Pre-configured OpenAI async client instance. |
| instruction_role | str | No | Role for instruction messages ("system" or "developer"). Default: "system". |
| middleware | Sequence | No | Chat and function middleware pipeline. |
| function_invocation_configuration | FunctionInvocationConfiguration | No | Override for function invocation behavior. |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | OpenAIResponsesClient | Configured client implementing SupportsChatGetResponse protocol. |
Usage Examples
Basic Initialization
from agent_framework.openai import OpenAIResponsesClient
# Uses OPENAI_API_KEY and OPENAI_RESPONSES_MODEL_ID env vars
client = OpenAIResponsesClient()
With Explicit Configuration
from agent_framework.openai import OpenAIResponsesClient
client = OpenAIResponsesClient(
model_id="gpt-4o",
api_key="sk-...",
org_id="org-...",
)
With Middleware
from agent_framework.openai import OpenAIResponsesClient
# Custom middleware for security filtering
class SecurityMiddleware:
async def __call__(self, context, next_handler):
# Filter sensitive content
return await next_handler(context)
client = OpenAIResponsesClient(
middleware=[SecurityMiddleware()],
)