Skip to content

Commit

Permalink
require base url if no api key
Browse files Browse the repository at this point in the history
  • Loading branch information
JONEMI21 committed Jan 30, 2025
1 parent dfb2779 commit 36a8028
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions pydantic_ai_slim/pydantic_ai/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass, field
from datetime import datetime, timezone
from itertools import chain
from typing import Literal, Union, cast, get_args, overload
from typing import Literal, Union, cast, overload

from httpx import AsyncClient as AsyncHTTPClient
from typing_extensions import assert_never
Expand Down Expand Up @@ -102,15 +102,9 @@ def __init__(
In the future, this may be inferred from the model name.
"""
self.model_name: OpenAIModelName = model_name
# This is a workaround for the OpenAI client requiring an API key, but locally served,
# openai compatible models models not always needing an API key.
if (
api_key is None
and 'OPENAI_API_KEY' not in os.environ
and model_name not in get_args(ChatModel)
and openai_client is None
):
assert base_url is not None, 'A base_url must be provided if not using an openai model'
# This is a workaround for the OpenAI client requiring an API key, whilst locally served,
# openai compatible models do not always need an API key.
if api_key is None and 'OPENAI_API_KEY' not in os.environ and base_url is not None:
api_key = ''
elif openai_client is not None:
assert http_client is None, 'Cannot provide both `openai_client` and `http_client`'
Expand Down

0 comments on commit 36a8028

Please sign in to comment.