77from typing import TYPE_CHECKING , Any , Literal , overload
88
99import httpx
10- from typing_extensions import deprecated
1110
1211from pydantic_ai .exceptions import UserError
1312from pydantic_ai .models import cached_async_http_client
2524
2625
2726@overload
28- @deprecated ('Use `chat` or `responses` API type instead of `openai` provider type.' )
2927def gateway_provider (
30- upstream_provider : Literal ['openai' , 'openai- chat' , 'openai- responses' ],
28+ api_type : Literal ['chat' , 'responses' ],
3129 / ,
3230 * ,
3331 api_key : str | None = None ,
@@ -38,76 +36,61 @@ def gateway_provider(
3836
3937@overload
4038def gateway_provider (
41- upstream_provider : Literal ['groq' ],
39+ api_key : Literal ['groq' ],
4240 / ,
4341 * ,
44- api_key : str | None = None ,
4542 base_url : str | None = None ,
4643 http_client : httpx .AsyncClient | None = None ,
4744) -> Provider [AsyncGroq ]: ...
4845
4946
5047@overload
51- @deprecated ('Use `gemini` or `anthropic` API type instead of `google-vertex` provider type.' )
5248def gateway_provider (
53- upstream_provider : Literal ['google-vertex ' ],
49+ api_type : Literal ['anthropic ' ],
5450 / ,
5551 * ,
5652 api_key : str | None = None ,
5753 base_url : str | None = None ,
58- ) -> Provider [GoogleClient ]: ...
54+ http_client : httpx .AsyncClient | None = None ,
55+ ) -> Provider [AsyncAnthropicClient ]: ...
5956
6057
6158@overload
6259def gateway_provider (
63- upstream_provider : Literal ['anthropic ' ],
60+ api_type : Literal ['converse ' ],
6461 / ,
6562 * ,
6663 api_key : str | None = None ,
6764 base_url : str | None = None ,
68- ) -> Provider [AsyncAnthropicClient ]: ...
65+ ) -> Provider [BaseClient ]: ...
6966
7067
7168@overload
72- @deprecated ('Use `converse` or `anthropic` API type instead of `bedrock` provider type.' )
7369def gateway_provider (
74- upstream_provider : Literal ['bedrock ' ],
70+ api_type : Literal ['gemini ' ],
7571 / ,
7672 * ,
7773 api_key : str | None = None ,
7874 base_url : str | None = None ,
79- ) -> Provider [BaseClient ]: ...
75+ http_client : httpx .AsyncClient | None = None ,
76+ ) -> Provider [GoogleClient ]: ...
8077
8178
8279@overload
8380def gateway_provider (
84- upstream_provider : str ,
81+ api_type : str ,
8582 / ,
8683 * ,
8784 api_key : str | None = None ,
8885 base_url : str | None = None ,
8986) -> Provider [Any ]: ...
9087
9188
92- UpstreamProvider = Literal [
93- 'chat' ,
94- 'responses' ,
95- 'gemini' ,
96- 'converse' ,
97- # Deprecated
98- 'openai' ,
99- 'openai-chat' ,
100- 'openai-responses' ,
101- 'google-vertex' ,
102- 'bedrock' ,
103- # Those two are actually the same on both.
104- 'anthropic' ,
105- 'groq' ,
106- ]
89+ APIType = Literal ['chat' , 'responses' , 'gemini' , 'converse' , 'anthropic' , 'groq' ]
10790
10891
10992def gateway_provider (
110- upstream_provider : UpstreamProvider | str ,
93+ api_type : APIType | str ,
11194 / ,
11295 * ,
11396 # Every provider
@@ -119,7 +102,7 @@ def gateway_provider(
119102 """Create a new Gateway provider.
120103
121104 Args:
122- upstream_provider: The upstream provider to use.
105+ api_type: Determines the API type to use.
123106 api_key: The API key to use for authentication. If not provided, the `PYDANTIC_AI_GATEWAY_API_KEY`
124107 environment variable will be used if available.
125108 base_url: The base URL to use for the Gateway. If not provided, the `PYDANTIC_AI_GATEWAY_BASE_URL`
@@ -134,23 +117,18 @@ def gateway_provider(
134117 )
135118
136119 base_url = base_url or os .getenv ('PYDANTIC_AI_GATEWAY_BASE_URL' , GATEWAY_BASE_URL )
137- http_client = http_client or cached_async_http_client (provider = f'gateway/{ upstream_provider } ' )
120+ http_client = http_client or cached_async_http_client (provider = f'gateway/{ api_type } ' )
138121 http_client .event_hooks = {'request' : [_request_hook (api_key )]}
139122
140- if upstream_provider in ('openai' , 'openai-chat' , 'openai-responses' ):
141- from .openai import OpenAIProvider
142-
143- return OpenAIProvider (api_key = api_key , base_url = _merge_url_path (base_url , 'openai' ), http_client = http_client )
144- elif upstream_provider in ('chat' , 'responses' ):
123+ if api_type in ('chat' , 'responses' ):
145124 from .openai import OpenAIProvider
146125
147- base_url = _merge_url_path (base_url , upstream_provider )
148- return OpenAIProvider (api_key = api_key , base_url = base_url , http_client = http_client )
149- elif upstream_provider == 'groq' :
126+ return OpenAIProvider (api_key = api_key , base_url = _merge_url_path (base_url , api_type ), http_client = http_client )
127+ elif api_type == 'groq' :
150128 from .groq import GroqProvider
151129
152130 return GroqProvider (api_key = api_key , base_url = _merge_url_path (base_url , 'groq' ), http_client = http_client )
153- elif upstream_provider == 'anthropic' :
131+ elif api_type == 'anthropic' :
154132 from anthropic import AsyncAnthropic
155133
156134 from .anthropic import AnthropicProvider
@@ -162,25 +140,25 @@ def gateway_provider(
162140 http_client = http_client ,
163141 )
164142 )
165- elif upstream_provider in ( 'bedrock' , ' converse') :
143+ elif api_type in ' converse' :
166144 from .bedrock import BedrockProvider
167145
168146 return BedrockProvider (
169147 api_key = api_key ,
170- base_url = _merge_url_path (base_url , upstream_provider ),
148+ base_url = _merge_url_path (base_url , api_type ),
171149 region_name = 'pydantic-ai-gateway' , # Fake region name to avoid NoRegionError
172150 )
173- elif upstream_provider in ( 'google-vertex' , ' gemini') :
151+ elif api_type in ' gemini' :
174152 from .google import GoogleProvider
175153
176154 return GoogleProvider (
177155 vertexai = True ,
178156 api_key = api_key ,
179- base_url = _merge_url_path (base_url , 'google-vertex ' ),
157+ base_url = _merge_url_path (base_url , 'gemini ' ),
180158 http_client = http_client ,
181159 )
182160 else :
183- raise UserError (f'Unknown upstream provider : { upstream_provider } ' )
161+ raise UserError (f'Unknown API type : { api_type } ' )
184162
185163
186164def _request_hook (api_key : str ) -> Callable [[httpx .Request ], Awaitable [httpx .Request ]]:
0 commit comments