Skip to content
Draft
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
40 changes: 35 additions & 5 deletions pydantic_ai_slim/pydantic_ai/providers/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import TYPE_CHECKING, Any, Literal, overload

import httpx
from typing_extensions import deprecated

from pydantic_ai.exceptions import UserError
from pydantic_ai.models import cached_async_http_client
Expand All @@ -24,8 +25,10 @@


@overload
@deprecated('Use `chat` or `responses` API type instead of `openai` provider type.')
def gateway_provider(
upstream_provider: Literal['openai', 'openai-chat', 'openai-responses'],
/,
*,
api_key: str | None = None,
base_url: str | None = None,
Expand All @@ -36,6 +39,7 @@ def gateway_provider(
@overload
def gateway_provider(
upstream_provider: Literal['groq'],
/,
*,
api_key: str | None = None,
base_url: str | None = None,
Expand All @@ -44,8 +48,10 @@ def gateway_provider(


@overload
@deprecated('Use `gemini` or `anthropic` API type instead of `google-vertex` provider type.')
def gateway_provider(
upstream_provider: Literal['google-vertex'],
/,
*,
api_key: str | None = None,
base_url: str | None = None,
Expand All @@ -55,15 +61,18 @@ def gateway_provider(
@overload
def gateway_provider(
upstream_provider: Literal['anthropic'],
/,
*,
api_key: str | None = None,
base_url: str | None = None,
) -> Provider[AsyncAnthropicClient]: ...


@overload
@deprecated('Use `converse` or `anthropic` API type instead of `bedrock` provider type.')
def gateway_provider(
upstream_provider: Literal['bedrock'],
/,
*,
api_key: str | None = None,
base_url: str | None = None,
Expand All @@ -73,22 +82,38 @@ def gateway_provider(
@overload
def gateway_provider(
upstream_provider: str,
/,
*,
api_key: str | None = None,
base_url: str | None = None,
) -> Provider[Any]: ...


UpstreamProvider = Literal['openai', 'openai-chat', 'openai-responses', 'groq', 'google-vertex', 'anthropic', 'bedrock']
UpstreamProvider = Literal[
'chat',
'responses',
'gemini',
'converse',
# Deprecated
'openai',
'openai-chat',
'openai-responses',
'google-vertex',
'bedrock',
# Those two are actually the same on both.
'anthropic',
'groq',
]


def gateway_provider(
upstream_provider: UpstreamProvider | str,
/,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change!!!

We do not document gateway_provider - I would like to follow the "if it's not documented, it's not public" approach.


We could also rename upstream_provider to api_type here.


If we are not okay with this, then the annoying path to deprecation it is...

*,
# Every provider
api_key: str | None = None,
base_url: str | None = None,
# OpenAI, Groq & Anthropic
# OpenAI, Groq, Anthropic & Gemini - Only Bedrock doesn't have an HTTPX client.
http_client: httpx.AsyncClient | None = None,
) -> Provider[Any]:
"""Create a new Gateway provider.
Expand Down Expand Up @@ -116,6 +141,11 @@ def gateway_provider(
from .openai import OpenAIProvider

return OpenAIProvider(api_key=api_key, base_url=_merge_url_path(base_url, 'openai'), http_client=http_client)
elif upstream_provider in ('chat', 'responses'):
from .openai import OpenAIProvider

base_url = _merge_url_path(base_url, upstream_provider)
return OpenAIProvider(api_key=api_key, base_url=base_url, http_client=http_client)
elif upstream_provider == 'groq':
from .groq import GroqProvider

Expand All @@ -132,15 +162,15 @@ def gateway_provider(
http_client=http_client,
)
)
elif upstream_provider == 'bedrock':
elif upstream_provider in ('bedrock', 'converse'):
from .bedrock import BedrockProvider

return BedrockProvider(
api_key=api_key,
base_url=_merge_url_path(base_url, 'bedrock'),
base_url=_merge_url_path(base_url, upstream_provider),
region_name='pydantic-ai-gateway', # Fake region name to avoid NoRegionError
)
elif upstream_provider == 'google-vertex':
elif upstream_provider in ('google-vertex', 'gemini'):
from .google import GoogleProvider

return GoogleProvider(
Expand Down
Loading