diff --git a/python/packages/openai/agent_framework_openai/_chat_client.py b/python/packages/openai/agent_framework_openai/_chat_client.py index 6592af38b9b..759748fdfdb 100644 --- a/python/packages/openai/agent_framework_openai/_chat_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_client.py @@ -833,6 +833,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") @@ -844,6 +846,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] @@ -4756,6 +4759,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. @@ -4779,6 +4783,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. """ ... @@ -4802,6 +4808,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. @@ -4832,6 +4839,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. """ ... @@ -4855,6 +4864,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. @@ -4895,6 +4905,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: @@ -4950,6 +4964,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 c0f41763a0d..8631cff798c 100644 --- a/python/packages/openai/agent_framework_openai/_chat_completion_client.py +++ b/python/packages/openai/agent_framework_openai/_chat_completion_client.py @@ -307,6 +307,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. @@ -329,9 +330,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. """ ... @@ -355,6 +356,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. @@ -384,9 +386,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. """ ... @@ -410,6 +412,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. @@ -455,6 +458,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: @@ -484,6 +491,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 @@ -506,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] @@ -1464,6 +1475,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. @@ -1510,6 +1522,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. @@ -1537,10 +1550,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. """ ... @@ -1563,6 +1578,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. @@ -1663,6 +1679,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, ) diff --git a/python/packages/openai/agent_framework_openai/_embedding_client.py b/python/packages/openai/agent_framework_openai/_embedding_client.py index 20823c0b957..815a2398226 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. @@ -228,6 +235,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 @@ -247,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) @@ -373,6 +386,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. @@ -391,6 +405,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. """ ... @@ -409,6 +425,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. @@ -434,6 +451,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. """ ... @@ -452,6 +471,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. @@ -487,6 +507,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: @@ -537,4 +561,5 @@ def __init__( otel_provider_name=otel_provider_name, env_file_path=env_file_path, env_file_encoding=env_file_encoding, + timeout=timeout, ) 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 397b98e6695..2ae048ae167 100644 --- a/python/packages/openai/tests/openai/test_openai_chat_client.py +++ b/python/packages/openai/tests/openai/test_openai_chat_client.py @@ -452,6 +452,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..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,6 +229,18 @@ def _prepare_extra_request_options(self, options: Any) -> dict[str, Any]: assert call_kwargs["dimensions"] == 8 +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]: