Skip to content

Add base_url to Mistral Provider #1617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/models/mistral.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ from pydantic_ai.models.mistral import MistralModel
from pydantic_ai.providers.mistral import MistralProvider

model = MistralModel(
'mistral-large-latest', provider=MistralProvider(api_key='your-api-key')
'mistral-large-latest', provider=MistralProvider(api_key='your-api-key', base_url='https://<mistral-provider-endpoint>')
)
agent = Agent(model)
...
Expand Down
7 changes: 5 additions & 2 deletions pydantic_ai_slim/pydantic_ai/providers/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
*,
api_key: str | None = None,
mistral_client: Mistral | None = None,
base_url: str | None = None,
http_client: AsyncHTTPClient | None = None,
) -> None:
"""Create a new Mistral provider.
Expand All @@ -52,11 +53,13 @@ def __init__(
api_key: The API key to use for authentication, if not provided, the `MISTRAL_API_KEY` environment variable
will be used if available.
mistral_client: An existing `Mistral` client to use, if provided, `api_key` and `http_client` must be `None`.
base_url: The base url for the Mistral requests.
http_client: An existing async client to use for making HTTP requests.
"""
if mistral_client is not None:
assert http_client is None, 'Cannot provide both `mistral_client` and `http_client`'
assert api_key is None, 'Cannot provide both `mistral_client` and `api_key`'
assert base_url is None, 'Cannot provide both `mistral_client` and `base_url`'
self._client = mistral_client
else:
api_key = api_key or os.environ.get('MISTRAL_API_KEY')
Expand All @@ -67,7 +70,7 @@ def __init__(
'to use the Mistral provider.'
)
elif http_client is not None:
self._client = Mistral(api_key=api_key, async_client=http_client)
self._client = Mistral(api_key=api_key, async_client=http_client, server_url=base_url)
else:
http_client = cached_async_http_client(provider='mistral')
self._client = Mistral(api_key=api_key, async_client=http_client)
self._client = Mistral(api_key=api_key, async_client=http_client, server_url=base_url)