Skip to content

Improve read timeout handling in HTTP client (#809) #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@
import logging
import platform
import email.utils
import httpx

class BaseClient:
def __init__(
self,
base_url: str,
headers: dict,
timeout_connect: float = 10.0,
timeout_read: float = 300.0,
timeout_write: float = 30.0,
timeout_pool: float = 30.0,
):
"""
Initialize the OpenAI API base client.

Args:
base_url (str): API base URL
headers (dict): Default request headers
timeout_connect (float): Connection timeout in seconds
timeout_read (float): Read timeout in seconds (allow longer for streaming/large responses)
timeout_write (float): Write timeout in seconds
timeout_pool (float): Pool acquisition timeout in seconds
"""
self._base_url = base_url
self._headers = headers

self._client = httpx.Client(
base_url=self._base_url,
headers=self._headers,
timeout=httpx.Timeout(
connect=timeout_connect,
read=timeout_read,
write=timeout_write,
pool=timeout_pool
)
)

from types import TracebackType
from random import random
from typing import (
Expand Down Expand Up @@ -38,6 +75,7 @@
from httpx import URL
from pydantic import PrivateAttr


from . import _exceptions
from ._qs import Querystring
from ._files import to_httpx_files, async_to_httpx_files
Expand Down