Skip to content
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
9 changes: 6 additions & 3 deletions src/agents/models/openai_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ def __init__(
chunk semantics are not reliable enough for incremental processing.
"""
if openai_client is not None:
if api_key is not None or base_url is not None or websocket_base_url is not None:
if any(
value is not None
for value in (api_key, base_url, websocket_base_url, organization, project)
):
raise UserError(
"Don't provide api_key, base_url, or websocket_base_url if you provide "
"openai_client"
"Don't provide api_key, base_url, websocket_base_url, organization, or project "
"if you provide openai_client"
)
self._client: AsyncOpenAI | None = openai_client
else:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_openai_provider_client_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

from typing import Any, cast

import pytest
from openai import AsyncOpenAI

from agents.exceptions import UserError
from agents.models.openai_provider import OpenAIProvider


@pytest.mark.parametrize(
"client_option",
[
{"organization": "org-test"},
{"project": "proj-test"},
],
)
def test_openai_provider_rejects_ignored_options_with_explicit_client(
client_option: dict[str, str],
) -> None:
client = cast(AsyncOpenAI, object())

with pytest.raises(UserError, match="organization, or project"):
OpenAIProvider(
openai_client=client,
**cast(dict[str, Any], client_option),
)
Loading