Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions python/packages/openai/agent_framework_openai/_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]

Expand Down Expand Up @@ -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.

Expand All @@ -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.
"""
...

Expand All @@ -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.

Expand Down Expand Up @@ -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.
"""
...

Expand All @@ -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,
Comment thread
IshamMohamed marked this conversation as resolved.
Resolved
) -> None:
"""Initialize an OpenAI Responses client.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Comment thread
IshamMohamed marked this conversation as resolved.
Resolved
middleware=middleware,
function_invocation_configuration=function_invocation_configuration,
compaction_strategy=compaction_strategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
"""
...

Expand All @@ -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.

Expand Down Expand Up @@ -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.
"""
...

Expand All @@ -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,
Comment thread
IshamMohamed marked this conversation as resolved.
Resolved
) -> None:
"""Initialize a raw OpenAI Chat completion client.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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]

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
"""
...

Expand All @@ -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.

Expand Down Expand Up @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
"""
...

Expand All @@ -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.

Expand All @@ -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.
"""
...

Expand All @@ -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.

Expand Down Expand Up @@ -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,
Comment thread
IshamMohamed marked this conversation as resolved.
Resolved
)

self.client = client
Expand All @@ -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)

Expand Down Expand Up @@ -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.

Expand All @@ -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.
"""
...

Expand All @@ -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.

Expand All @@ -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.
"""
...

Expand All @@ -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.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Comment thread
IshamMohamed marked this conversation as resolved.
Resolved
)
12 changes: 12 additions & 0 deletions python/packages/openai/tests/openai/test_openai_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
Loading