44
55import os
66from typing import TYPE_CHECKING , Any , Literal , overload
7- from urllib .parse import urljoin
87
98import httpx
109
@@ -84,22 +83,22 @@ def gateway_provider(
8483 ' to use the Pydantic AI Gateway provider.'
8584 )
8685
87- base_url = base_url or os .getenv ('PYDANTIC_AI_GATEWAY_BASE_URL' , 'http://localhost:8787' )
86+ base_url = base_url or os .getenv ('PYDANTIC_AI_GATEWAY_BASE_URL' , 'http://localhost:8787/proxy ' )
8887 http_client = http_client or cached_async_http_client (provider = f'gateway-{ upstream_provider } ' )
8988 http_client .event_hooks = {'request' : [_request_hook ]}
9089
9190 if upstream_provider in ('openai' , 'openai-chat' ):
9291 from .openai import OpenAIProvider
9392
94- return OpenAIProvider (api_key = api_key , base_url = urljoin (base_url , 'openai' ), http_client = http_client )
93+ return OpenAIProvider (api_key = api_key , base_url = _merge_url_path (base_url , 'openai' ), http_client = http_client )
9594 elif upstream_provider == 'openai-responses' :
9695 from .openai import OpenAIProvider
9796
98- return OpenAIProvider (api_key = api_key , base_url = urljoin (base_url , 'openai' ), http_client = http_client )
97+ return OpenAIProvider (api_key = api_key , base_url = _merge_url_path (base_url , 'openai' ), http_client = http_client )
9998 elif upstream_provider == 'groq' :
10099 from .groq import GroqProvider
101100
102- return GroqProvider (api_key = api_key , base_url = urljoin (base_url , 'groq' ), http_client = http_client )
101+ return GroqProvider (api_key = api_key , base_url = _merge_url_path (base_url , 'groq' ), http_client = http_client )
103102 elif upstream_provider == 'anthropic' :
104103 from anthropic import AsyncAnthropic
105104
@@ -108,7 +107,7 @@ def gateway_provider(
108107 return AnthropicProvider (
109108 anthropic_client = AsyncAnthropic (
110109 auth_token = api_key ,
111- base_url = urljoin (base_url , 'anthropic' ),
110+ base_url = _merge_url_path (base_url , 'anthropic' ),
112111 http_client = http_client ,
113112 )
114113 )
@@ -122,7 +121,7 @@ def gateway_provider(
122121 vertexai = True ,
123122 api_key = 'unset' ,
124123 http_options = {
125- 'base_url' : f' { base_url } / google-vertex' ,
124+ 'base_url' : _merge_url_path ( base_url , ' google-vertex') ,
126125 'headers' : {'User-Agent' : get_user_agent (), 'Authorization' : api_key },
127126 # TODO(Marcelo): Until https://github.com/googleapis/python-genai/issues/1357 is solved.
128127 'async_client_args' : {
@@ -185,3 +184,13 @@ async def _request_hook(request: httpx.Request) -> httpx.Request:
185184 request .headers .update (headers )
186185
187186 return request
187+
188+
189+ def _merge_url_path (base_url : str , path : str ) -> str :
190+ """Merge a base URL and a path.
191+
192+ Args:
193+ base_url: The base URL to merge.
194+ path: The path to merge.
195+ """
196+ return base_url .rstrip ('/' ) + '/' + path .lstrip ('/' )
0 commit comments