Skip to content
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
18 changes: 15 additions & 3 deletions pydantic_ai_slim/pydantic_ai/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations as _annotations

import re
from abc import ABC, abstractmethod
from typing import Any, Generic, TypeVar

Expand Down Expand Up @@ -153,13 +154,24 @@ def infer_provider_class(provider: str) -> type[Provider[Any]]: # noqa: C901
raise ValueError(f'Unknown provider: {provider}')


_gateway_provider_regex = re.compile('gateway(?::([^/]*))?/(.*)')
"""
This regex matches strings of the forms:
1. gateway/my-upstream-provider
2. gateway:my-route/my-upstream-provider
Copy link
Collaborator

Choose a reason for hiding this comment

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

infer_provider is used in infer_model, which already uses : as a separator. So gateway:my-route/my-upstream-provider:my-model would be ambiguous. Some model providers even use : (and of course /) in their model name.

I don't think we should support something here that doesn't work as a provider shorthand for infer_model.

Would gateway/my-upstream-provider@my-route be crazy?


The (optional) route is the first group, the upstream provider is the second.
"""


def infer_provider(provider: str) -> Provider[Any]:
"""Infer the provider from the provider name."""
if provider.startswith('gateway/'):
if match := re.match(_gateway_provider_regex, provider):
from .gateway import gateway_provider

upstream_provider = provider.removeprefix('gateway/')
return gateway_provider(upstream_provider)
route = match.group(1)
upstream_provider = match.group(2)
return gateway_provider(upstream_provider, route=route)
elif provider in ('google-vertex', 'google-gla'):
from .google import GoogleProvider

Expand Down