Repository navigation
Python: Add timeout parameter to OpenAI client classes - #9924
Isham Mohamed (IshamMohamed) wants to merge 6 commits into
Conversation
(OpenAIChatClient, OpenAIChatCompletionClient, and OpenAIEmbeddingClient) for consistent HTTP timeout control across all OpenAI integrations.
There was a problem hiding this comment.
🟡 Changes recommended
A constructor indentation error prevents Chat Completions imports, while missing public timeout support and superclass forwarding leave the new timeout behavior incomplete.
4 open issues
What changed in this PR
This PR aims to address #9923 by exposing HTTP timeout control through Agent Framework’s public Python OpenAI clients, without requiring a custom SDK client.
Changes:
- Adds timeout parameters to Responses and embedding client constructors, and the raw Chat Completions constructor.
- Forwards embedding and raw Chat Completions timeouts through shared settings.
- Documents timeout defaults and behavior when an SDK client is supplied.
| File | Description |
|---|---|
python/packages/openai/agent_framework_openai/_embedding_client.py |
Adds timeout parameters, documentation, and forwarding for raw and public embedding clients. |
python/packages/openai/agent_framework_openai/_chat_completion_client.py |
Adds timeout support to the raw Chat Completions constructor. |
python/packages/openai/agent_framework_openai/_chat_client.py |
Adds timeout parameters and documentation to the public Responses client. |
Review effort: Balanced
…ation to restore imports' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| instruction_role=instruction_role, | ||
| env_file_path=env_file_path, | ||
| env_file_encoding=env_file_encoding, | ||
| timeout=timeout, |
There was a problem hiding this comment.
should we check if it is a positive number? or will the underlying client raise when that is not the case?
There was a problem hiding this comment.
The most underlying sockets will throw a ValueError
Currently the following is how the request is handled concerning timeout (roughly) RawOpenAIChatClient -> httpx -> httpcore -> socket timeout. There is no validation done until httpcore, but Python's socket module throws following exception when negative timeout is passed along the line:
ValueError: Timeout value out of range


(OpenAIChatClient, OpenAIChatCompletionClient, and OpenAIEmbeddingClient) for consistent HTTP timeout control across all OpenAI integrations.
Motivation & Context
Why is this change required?
This change is required because the high-level OpenAI client classes (
OpenAIChatClient,OpenAIChatCompletionClient,OpenAIEmbeddingClient) did not expose atimeoutparameter, while the underlying raw clients (RawOpenAIChatClient,RawOpenAIChatCompletionClient,RawOpenAIEmbeddingClient) already supported it. This prevented developers from controlling HTTP timeouts when using the framework's public client APIs.What problem does it solve?
It solves the problem of developers being unable to override the default OpenAI SDK timeout (connect: 5s, read/write/pool: 600s) through the framework's high-level clients. Previously, developers had to manually configure
async_clientinstances to control timeouts, which was inconsistent with the raw client API.What scenario does it contribute to?
It contributes to the scenario where applications need different timeout behaviors for OpenAI API requests (chat, chat completions, embeddings) without having to manually configure
async_clientinstances. For example, a long-running embedding request might need a 120s timeout while chat requests use 30s.If it fixes an open issue, please link to the issue below.
Fixes Python: [Feature]: Make timeout configurable on framework OpenAI clients (chat, chat completion, embedding) #9923
Scope of work is only limited to
timeouton the OpenAI client classes because it depends onRawOpenAI*Clientclasses and they havetimeoutexposed. A seperate PR may be requirered to implementmax_retries(similar to Python: [Feature]: Add max_retries (or OpenAI client kwargs) to FoundryChatClient #9719) because that work requiresRawOpenAI*Clienttoo to havemax_retriesexposed.Description & Review Guide
The overall approach follows the existing pattern already established in the raw OpenAI client classes (
RawOpenAIChatClient,RawOpenAIChatCompletionClient,RawOpenAIEmbeddingClient), which already supported atimeoutparameter. The implementation uses theload_openai_service_settings()infrastructure to handle both OpenAI and Azure routing consistently.The underlying design ensures a single
timeoutvalue applies to all request phases (connect, read, write, pool). Whenasync_clientis provided, the timeout parameter is ignored, allowing users to configure timeout directly on their pre-configured client. Default behavior is preserved (None= OpenAI SDK defaults: connect 5s, read/write/pool 600s).Added
timeout: float | None = Noneto all three__init__overloads ofOpenAIChatClient,OpenAIChatCompletionClient, andOpenAIEmbeddingClient. Updated docstrings to document the timeout parameter. Forwarded timeout throughload_openai_service_settings()for proper handling.Developers can now pass
timeoutto all three client classes. Default behavior is preserved (None = SDK default). Timeout is ignored whenasync_clientis provided. (PS: not added toNvidiaEmbeddingClientorFoundryLocalClientas they manage their own clients). The focust is only on directly OpenAI related clients on the agent-framework.timeoutis added to all three overloads for each client classtimeout=timeoutis passed toload_openai_service_settings()Nonepreserves SDK defaults)Related Issue
Fixes #9923
AI Assistance
AI assistance details: Used openrouter/free model.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.