From 48575eae76bd10b150c1f42c98813208df9c04cb Mon Sep 17 00:00:00 2001 From: isham Date: Sat, 31 Oct 2026 23:44:10 +0000 Subject: [PATCH 1/5] Python: Add timeout parameter to OpenAI client classes (OpenAIChatClient, OpenAIChatCompletionClient, and OpenAIEmbeddingClient) for consistent HTTP timeout control across all OpenAI integrations. --- .../agent_framework_openai/_chat_client.py | 11 +++++++ .../_chat_completion_client.py | 30 ++++++++++++------- .../_embedding_client.py | 20 +++++++++++++ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/python/packages/openai/agent_framework_openai/_chat_client.py b/python/packages/openai/agent_framework_openai/_chat_client.py index 1e0255ff6ce..dc405a1d9b0 100644 --- a/python/packages/openai/agent_framework_openai/_chat_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_client.py @@ -4726,6 +4726,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI Responses client. @@ -4749,6 +4750,8 @@ def __init__( env_file_path: Optional ``.env`` file that is checked before the process environment for ``OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -4772,6 +4775,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI Responses client. @@ -4802,6 +4806,8 @@ def __init__( env_file_path: Optional ``.env`` file that is checked before process environment variables for ``AZURE_OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -4825,6 +4831,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI Responses client. @@ -4863,6 +4870,10 @@ def __init__( variables. The same file is used for both ``OPENAI_*`` and ``AZURE_OPENAI_*`` lookups. env_file_encoding: Encoding for the ``.env`` file. + timeout: HTTP timeout in seconds for requests. When not provided, the OpenAI SDK + default is used (connect: 5s, total: 600s). A single value applies to + every phase, including connect. Ignored when ``async_client`` is provided; + configure the timeout on that client instead. Notes: Environment resolution and routing precedence are: diff --git a/python/packages/openai/agent_framework_openai/_chat_completion_client.py b/python/packages/openai/agent_framework_openai/_chat_completion_client.py index 27da4fea330..91309f4912c 100644 --- a/python/packages/openai/agent_framework_openai/_chat_completion_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_completion_client.py @@ -311,13 +311,14 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, - ) -> None: - """Initialize a raw OpenAI Chat completion client. - - Keyword Args: - model: Model identifier to use for the request. When not provided, the constructor - reads ``OPENAI_CHAT_COMPLETION_MODEL`` and then ``OPENAI_MODEL``. - api_key: API key. When not provided explicitly, the constructor reads + timeout: float | None = None, +) -> None: + """Initialize a raw OpenAI Chat completion client. + + Keyword Args: + model: Model identifier to use for the request. When not provided, the constructor + reads ``OPENAI_CHAT_COMPLETION_MODEL`` and then ``OPENAI_MODEL``. + api_key: API key. When not provided explicitly, the constructor reads ``OPENAI_API_KEY``. A callable API key is also supported. org_id: OpenAI organization ID. When not provided explicitly, the constructor reads ``OPENAI_ORG_ID``. @@ -333,9 +334,9 @@ def __init__( compaction_strategy: Optional per-client compaction override. tokenizer: Optional tokenizer for compaction strategies. additional_properties: Additional properties stored on the client instance. - env_file_path: Optional ``.env`` file that is checked before the process environment - for ``OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -359,6 +360,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize a raw OpenAI Chat completion client. @@ -388,9 +390,9 @@ def __init__( compaction_strategy: Optional per-client compaction override. tokenizer: Optional tokenizer for compaction strategies. additional_properties: Additional properties stored on the client instance. - env_file_path: Optional ``.env`` file that is checked before process environment - variables for ``AZURE_OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -414,6 +416,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize a raw OpenAI Chat completion client. @@ -456,6 +459,10 @@ def __init__( variables. The same file is used for both ``OPENAI_*`` and ``AZURE_OPENAI_*`` lookups. env_file_encoding: Encoding for the ``.env`` file. + timeout: HTTP timeout in seconds for requests. When not provided, the OpenAI SDK + default is used (connect: 5s, read/write/pool: 600s). A single value applies to + every phase, including connect. Ignored when ``async_client`` is provided; + configure the timeout on that client instead. Notes: Environment resolution and routing precedence are: @@ -485,6 +492,7 @@ def __init__( env_file_encoding=env_file_encoding, openai_model_fields=("chat_completion_model", "model"), azure_model_fields=("chat_completion_model", "model"), + timeout=timeout, ) self.client = client diff --git a/python/packages/openai/agent_framework_openai/_embedding_client.py b/python/packages/openai/agent_framework_openai/_embedding_client.py index 9560f99cc91..d1b2591322e 100644 --- a/python/packages/openai/agent_framework_openai/_embedding_client.py +++ b/python/packages/openai/agent_framework_openai/_embedding_client.py @@ -85,6 +85,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize a raw OpenAI embedding client. @@ -103,6 +104,8 @@ def __init__( env_file_path: Optional ``.env`` file that is checked before the process environment for ``OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -121,6 +124,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize a raw OpenAI embedding client. @@ -146,6 +150,8 @@ def __init__( env_file_path: Optional ``.env`` file that is checked before process environment variables for ``AZURE_OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -164,6 +170,7 @@ def __init__( additional_properties: dict[str, Any] | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize a raw OpenAI embedding client. @@ -226,6 +233,7 @@ def __init__( env_file_encoding=env_file_encoding, openai_model_fields=("embedding_model", "model"), azure_model_fields=("embedding_model", "model"), + timeout=timeout, ) self.client = client @@ -371,6 +379,7 @@ def __init__( otel_provider_name: str | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI embedding client. @@ -389,6 +398,8 @@ def __init__( env_file_path: Optional ``.env`` file that is checked before the process environment for ``OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -407,6 +418,7 @@ def __init__( otel_provider_name: str | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI embedding client. @@ -432,6 +444,8 @@ def __init__( env_file_path: Optional ``.env`` file that is checked before process environment variables for ``AZURE_OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -450,6 +464,7 @@ def __init__( otel_provider_name: str | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI embedding client. @@ -483,6 +498,10 @@ def __init__( variables. The same file is used for both ``OPENAI_*`` and ``AZURE_OPENAI_*`` lookups. env_file_encoding: Encoding for the ``.env`` file. + timeout: HTTP timeout in seconds for requests. When not provided, the OpenAI SDK + default is used (connect: 5s, read/write/pool: 600s). A single value applies to + every phase, including connect. Ignored when ``async_client`` is provided; + configure the timeout on that client instead. Notes: Environment resolution precedence is: @@ -533,4 +552,5 @@ def __init__( otel_provider_name=otel_provider_name, env_file_path=env_file_path, env_file_encoding=env_file_encoding, + timeout=timeout, ) From d0d64c90cbeb0dbee7700f122519c2c37cde9f45 Mon Sep 17 00:00:00 2001 From: Isham Mohamed Date: Sun, 1 Nov 2026 01:13:07 +0000 Subject: [PATCH 2/5] Potential fix for pull request finding 'Fix overload docstring indentation to restore imports' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../_chat_completion_client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/packages/openai/agent_framework_openai/_chat_completion_client.py b/python/packages/openai/agent_framework_openai/_chat_completion_client.py index 91309f4912c..04061de4fdb 100644 --- a/python/packages/openai/agent_framework_openai/_chat_completion_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_completion_client.py @@ -312,13 +312,13 @@ def __init__( env_file_path: str | None = None, env_file_encoding: str | None = None, timeout: float | None = None, -) -> None: - """Initialize a raw OpenAI Chat completion client. + ) -> None: + """Initialize a raw OpenAI Chat completion client. - Keyword Args: - model: Model identifier to use for the request. When not provided, the constructor - reads ``OPENAI_CHAT_COMPLETION_MODEL`` and then ``OPENAI_MODEL``. - api_key: API key. When not provided explicitly, the constructor reads + Keyword Args: + model: Model identifier to use for the request. When not provided, the constructor + reads ``OPENAI_CHAT_COMPLETION_MODEL`` and then ``OPENAI_MODEL``. + api_key: API key. When not provided explicitly, the constructor reads ``OPENAI_API_KEY``. A callable API key is also supported. org_id: OpenAI organization ID. When not provided explicitly, the constructor reads ``OPENAI_ORG_ID``. From 1bf5688f3e05162fb6e6e5e533646481197cd307 Mon Sep 17 00:00:00 2001 From: isham Date: Sun, 1 Nov 2026 01:36:03 +0000 Subject: [PATCH 3/5] Python: Fixed timeout passing to superclass --- .../openai/agent_framework_openai/_chat_client.py | 1 + .../agent_framework_openai/_chat_completion_client.py | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/python/packages/openai/agent_framework_openai/_chat_client.py b/python/packages/openai/agent_framework_openai/_chat_client.py index dc405a1d9b0..06cab8c9586 100644 --- a/python/packages/openai/agent_framework_openai/_chat_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_client.py @@ -4929,6 +4929,7 @@ class MyOptions(OpenAIChatOptions, total=False): instruction_role=instruction_role, env_file_path=env_file_path, env_file_encoding=env_file_encoding, + timeout=timeout, middleware=middleware, function_invocation_configuration=function_invocation_configuration, compaction_strategy=compaction_strategy, diff --git a/python/packages/openai/agent_framework_openai/_chat_completion_client.py b/python/packages/openai/agent_framework_openai/_chat_completion_client.py index 04061de4fdb..94d0774d017 100644 --- a/python/packages/openai/agent_framework_openai/_chat_completion_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_completion_client.py @@ -1473,6 +1473,7 @@ def __init__( env_file_encoding: str | None = None, middleware: Sequence[ChatAndFunctionMiddlewareTypes] | None = None, function_invocation_configuration: FunctionInvocationConfiguration | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI Chat completion client. @@ -1519,6 +1520,7 @@ def __init__( env_file_encoding: str | None = None, middleware: Sequence[ChatAndFunctionMiddlewareTypes] | None = None, function_invocation_configuration: FunctionInvocationConfiguration | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI Chat completion client. @@ -1546,10 +1548,12 @@ def __init__( message_preparer: Optional hook to customize outgoing request messages. See ``OpenAIChatMessagePreparer``. env_file_path: Optional ``.env`` file that is checked before process environment - variables for ``AZURE_OPENAI_*`` values. + for ``AZURE_OPENAI_*`` values. env_file_encoding: Encoding for the ``.env`` file. middleware: Optional sequence of ChatAndFunctionMiddlewareTypes to apply to requests. function_invocation_configuration: Optional configuration for function invocation support. + timeout: Optional HTTP timeout in seconds for requests. Ignored when ``async_client`` + is provided; configure the timeout on that client instead. """ ... @@ -1572,6 +1576,7 @@ def __init__( function_invocation_configuration: FunctionInvocationConfiguration | None = None, env_file_path: str | None = None, env_file_encoding: str | None = None, + timeout: float | None = None, ) -> None: """Initialize an OpenAI Chat completion client. @@ -1670,6 +1675,7 @@ class MyOptions(OpenAIChatCompletionOptions, total=False): message_preparer=message_preparer, env_file_path=env_file_path, env_file_encoding=env_file_encoding, + timeout=timeout, middleware=middleware, function_invocation_configuration=function_invocation_configuration, ) From ec7cd28468b2ee4c0f5bb53d761d4659ebe11a74 Mon Sep 17 00:00:00 2001 From: isham Date: Thu, 5 Nov 2026 23:08:09 +0000 Subject: [PATCH 4/5] Python: Store timeout on raw OpenAI client instances for serialization The raw OpenAI client constructors (`RawOpenAIChatClient`, `RawOpenAIChatCompletionClient`, `RawOpenAIEmbeddingClient`) accepted a `timeout` parameter but did not store it as an instance attribute. As a result, `to_dict()` / `from_dict()` round-trips silently dropped the configured timeout, reverting to the OpenAI SDK default (600s). Store `self.timeout = timeout` on each raw client so the timeout is preserved through serialization. Add round-trip tests for all three clients to verify the behavior. --- .../openai/agent_framework_openai/_chat_client.py | 3 +++ .../_chat_completion_client.py | 3 +++ .../agent_framework_openai/_embedding_client.py | 5 +++++ .../openai/tests/openai/test_openai_chat_client.py | 12 ++++++++++++ .../openai/test_openai_chat_completion_client.py | 12 ++++++++++++ .../tests/openai/test_openai_embedding_client.py | 14 ++++++++++++++ 6 files changed, 49 insertions(+) diff --git a/python/packages/openai/agent_framework_openai/_chat_client.py b/python/packages/openai/agent_framework_openai/_chat_client.py index e6686ab92ec..3b19000ea50 100644 --- a/python/packages/openai/agent_framework_openai/_chat_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_client.py @@ -831,6 +831,8 @@ def __init__( self.model: str = settings.get("model") or "" # Store configuration for serialization + # Settings dict contains OpenAI/Azure configuration values (model, org_id, base_url, etc.) + # Client behavior parameters like timeout are stored directly as instance attributes self.org_id = settings.get("org_id") self.base_url = settings.get("base_url") self.azure_endpoint = settings.get("endpoint") @@ -842,6 +844,7 @@ def __init__( else: self.default_headers = None self.instruction_role = instruction_role + self.timeout = timeout if use_azure_client: self.OTEL_PROVIDER_NAME = "azure.ai.openai" # type: ignore[misc] diff --git a/python/packages/openai/agent_framework_openai/_chat_completion_client.py b/python/packages/openai/agent_framework_openai/_chat_completion_client.py index 845966ad350..8631cff798c 100644 --- a/python/packages/openai/agent_framework_openai/_chat_completion_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_completion_client.py @@ -514,6 +514,9 @@ def __init__( self.response_parser = response_parser self.message_preparer = message_preparer self._use_azure_client = use_azure_client + # Settings dict contains OpenAI/Azure configuration values (model, org_id, base_url, etc.) + # Client behavior parameters like timeout are stored directly as instance attributes + self.timeout = timeout if use_azure_client: self.OTEL_PROVIDER_NAME = "azure.ai.openai" # type: ignore[misc] diff --git a/python/packages/openai/agent_framework_openai/_embedding_client.py b/python/packages/openai/agent_framework_openai/_embedding_client.py index bbaffe92a81..815a2398226 100644 --- a/python/packages/openai/agent_framework_openai/_embedding_client.py +++ b/python/packages/openai/agent_framework_openai/_embedding_client.py @@ -255,6 +255,11 @@ def __init__( self.default_headers = None if use_azure_client: self.OTEL_PROVIDER_NAME = "azure.ai.openai" # type: ignore[misc] + + # Store configuration for serialization + # Settings dict contains OpenAI/Azure configuration values (model, org_id, base_url, etc.) + # Client behavior parameters like timeout are stored directly as instance attributes + self.timeout = timeout super().__init__(additional_properties=additional_properties) diff --git a/python/packages/openai/tests/openai/test_openai_chat_client.py b/python/packages/openai/tests/openai/test_openai_chat_client.py index 445958e354f..720fde319a5 100644 --- a/python/packages/openai/tests/openai/test_openai_chat_client.py +++ b/python/packages/openai/tests/openai/test_openai_chat_client.py @@ -451,6 +451,18 @@ def test_serialize(openai_unit_test_env: dict[str, str]) -> None: assert "User-Agent" not in dumped_settings["default_headers"] +def test_serialize_with_timeout(openai_unit_test_env: dict[str, str]) -> None: + settings = { + "model": openai_unit_test_env["OPENAI_MODEL"], + "api_key": openai_unit_test_env["OPENAI_API_KEY"], + "timeout": 30.0, + } + + client = OpenAIChatClient.from_dict(settings) + dumped_settings = client.to_dict() + assert dumped_settings["timeout"] == 30.0 + + def test_serialize_with_org_id(openai_unit_test_env: dict[str, str]) -> None: settings = { "model": openai_unit_test_env["OPENAI_MODEL"], diff --git a/python/packages/openai/tests/openai/test_openai_chat_completion_client.py b/python/packages/openai/tests/openai/test_openai_chat_completion_client.py index 7d5fc75b077..ff9e1d1befd 100644 --- a/python/packages/openai/tests/openai/test_openai_chat_completion_client.py +++ b/python/packages/openai/tests/openai/test_openai_chat_completion_client.py @@ -233,6 +233,18 @@ def test_serialize_with_org_id(openai_unit_test_env: dict[str, str]) -> None: assert "User-Agent" not in dumped_settings.get("default_headers", {}) +def test_serialize_with_timeout(openai_unit_test_env: dict[str, str]) -> None: + settings = { + "model": openai_unit_test_env["OPENAI_MODEL"], + "api_key": openai_unit_test_env["OPENAI_API_KEY"], + "timeout": 30.0, + } + + client = OpenAIChatCompletionClient.from_dict(settings) + dumped_settings = client.to_dict() + assert dumped_settings["timeout"] == 30.0 + + @pytest.mark.parametrize( ("inner_code", "expected_code"), [ diff --git a/python/packages/openai/tests/openai/test_openai_embedding_client.py b/python/packages/openai/tests/openai/test_openai_embedding_client.py index cf2cb071a0d..521991630cf 100644 --- a/python/packages/openai/tests/openai/test_openai_embedding_client.py +++ b/python/packages/openai/tests/openai/test_openai_embedding_client.py @@ -229,6 +229,20 @@ def _prepare_extra_request_options(self, options: Any) -> dict[str, Any]: assert call_kwargs["dimensions"] == 8 +@pytest.mark.flaky +@pytest.mark.integration +async def test_serialize_with_timeout(openai_unit_test_env: dict[str, str]) -> None: + settings = { + "model": openai_unit_test_env["OPENAI_EMBEDDING_MODEL"], + "api_key": openai_unit_test_env["OPENAI_API_KEY"], + "timeout": 45.0, + } + + client = OpenAIEmbeddingClient.from_dict(settings) + dumped_settings = client.to_dict() + assert dumped_settings["timeout"] == 45.0 + + async def test_rejected_extra_request_options_send_nothing(openai_unit_test_env: dict[str, str]) -> None: class CompatibleEmbeddingClient(OpenAIEmbeddingClient): def _prepare_extra_request_options(self, options: Any) -> dict[str, Any]: From 63e02618db6ec22feab3431fe3d8793681593324 Mon Sep 17 00:00:00 2001 From: isham Date: Wed, 11 Nov 2026 12:45:44 +0000 Subject: [PATCH 5/5] Python: feat(openai): remove integration and flaky markers from test_serialize_with_timeout test because previously it was overly conservative --- .../openai/tests/openai/test_openai_embedding_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/packages/openai/tests/openai/test_openai_embedding_client.py b/python/packages/openai/tests/openai/test_openai_embedding_client.py index 521991630cf..7051489e6fd 100644 --- a/python/packages/openai/tests/openai/test_openai_embedding_client.py +++ b/python/packages/openai/tests/openai/test_openai_embedding_client.py @@ -229,8 +229,6 @@ def _prepare_extra_request_options(self, options: Any) -> dict[str, Any]: assert call_kwargs["dimensions"] == 8 -@pytest.mark.flaky -@pytest.mark.integration async def test_serialize_with_timeout(openai_unit_test_env: dict[str, str]) -> None: settings = { "model": openai_unit_test_env["OPENAI_EMBEDDING_MODEL"],