@@ -49,6 +49,8 @@ class OpenAIHTTPBackend(Backend):
4949 If not provided, the default timeout provided from settings is used.
5050 :param http2: If True, uses HTTP/2 for requests to the OpenAI server.
5151 Defaults to True.
52+ :param follow_redirects: If True, the HTTP client will follow redirect responses.
53+ If not provided, the default value from settings is used.
5254 :param max_output_tokens: The maximum number of tokens to request for completions.
5355 If not provided, the default maximum tokens provided from settings is used.
5456 """
@@ -62,6 +64,7 @@ def __init__(
6264 project : Optional [str ] = None ,
6365 timeout : Optional [float ] = None ,
6466 http2 : Optional [bool ] = True ,
67+ follow_redirects : Optional [bool ] = None ,
6568 max_output_tokens : Optional [int ] = None ,
6669 ):
6770 super ().__init__ (type_ = "openai_http" )
@@ -88,6 +91,11 @@ def __init__(
8891 self .project = project or settings .openai .project
8992 self .timeout = timeout if timeout is not None else settings .request_timeout
9093 self .http2 = http2 if http2 is not None else settings .request_http2
94+ self .follow_redirects = (
95+ follow_redirects
96+ if follow_redirects is not None
97+ else settings .request_follow_redirects
98+ )
9199 self .max_output_tokens = (
92100 max_output_tokens
93101 if max_output_tokens is not None
@@ -120,6 +128,7 @@ def info(self) -> dict[str, Any]:
120128 "max_output_tokens" : self .max_output_tokens ,
121129 "timeout" : self .timeout ,
122130 "http2" : self .http2 ,
131+ "follow_redirects" : self .follow_redirects ,
123132 "authorization" : bool (self .authorization ),
124133 "organization" : self .organization ,
125134 "project" : self .project ,
@@ -319,7 +328,11 @@ def _get_async_client(self) -> httpx.AsyncClient:
319328 :return: The async HTTP client.
320329 """
321330 if self ._async_client is None :
322- client = httpx .AsyncClient (http2 = self .http2 , timeout = self .timeout )
331+ client = httpx .AsyncClient (
332+ http2 = self .http2 ,
333+ timeout = self .timeout ,
334+ follow_redirects = self .follow_redirects ,
335+ )
323336 self ._async_client = client
324337 else :
325338 client = self ._async_client
@@ -449,12 +462,13 @@ async def _iterative_completions_request(
449462 raise ValueError (f"Unsupported type: { type_ } " )
450463
451464 logger .info (
452- "{} making request: {} to target: {} using http2: {} for "
453- "timeout: {} with headers: {} and payload: {}" ,
465+ "{} making request: {} to target: {} using http2: {} following "
466+ "redirects: {} for timeout: {} with headers: {} and payload: {}" ,
454467 self .__class__ .__name__ ,
455468 request_id ,
456469 target ,
457470 self .http2 ,
471+ self .follow_redirects ,
458472 self .timeout ,
459473 headers ,
460474 payload ,
@@ -544,6 +558,7 @@ async def _iterative_completions_request(
544558 payload = payload ,
545559 timeout = self .timeout ,
546560 http2 = self .http2 ,
561+ follow_redirects = self .follow_redirects ,
547562 ),
548563 start_time = start_time ,
549564 end_time = iter_time ,
0 commit comments