Python: OpenAI Clients accepting api_key callback (#1139)

* openai clients accepting callable

* fixes

* version checks
This commit is contained in:
Giles Odigwe
2025-10-06 12:51:34 -07:00
committed by GitHub
Unverified
parent ee8d95b3dc
commit 4e6ae443cf
7 changed files with 107 additions and 14 deletions
@@ -1258,3 +1258,18 @@ async def test_openai_assistants_client_agent_level_tool_persistence():
assert second_response.text is not None
# Should use the agent-level weather tool again
assert any(term in second_response.text.lower() for term in ["miami", "sunny", "72"])
# Callable API Key Tests
def test_openai_assistants_client_with_callable_api_key() -> None:
"""Test OpenAIAssistantsClient initialization with callable API key."""
async def get_api_key() -> str:
return "test-api-key-123"
client = OpenAIAssistantsClient(model_id="gpt-4o", api_key=get_api_key)
# Verify client was created successfully
assert client.model_id == "gpt-4o"
# OpenAI SDK now manages callable API keys internally
assert client.client is not None
@@ -783,3 +783,17 @@ def test_openai_content_parser_data_content_image(openai_unit_test_env: dict[str
# Should use custom filename
assert result["type"] == "file"
assert result["file"]["filename"] == "report.pdf"
def test_openai_chat_client_with_callable_api_key() -> None:
"""Test OpenAIChatClient initialization with callable API key."""
async def get_api_key() -> str:
return "test-api-key-123"
client = OpenAIChatClient(model_id="gpt-4o", api_key=get_api_key)
# Verify client was created successfully
assert client.model_id == "gpt-4o"
# OpenAI SDK now manages callable API keys internally
assert client.client is not None
@@ -2088,3 +2088,17 @@ def test_prepare_options_store_parameter_handling() -> None:
options = client._prepare_options(messages, chat_options) # type: ignore
assert options["store"] is False
assert "previous_response_id" not in options
def test_openai_responses_client_with_callable_api_key() -> None:
"""Test OpenAIResponsesClient initialization with callable API key."""
async def get_api_key() -> str:
return "test-api-key-123"
client = OpenAIResponsesClient(model_id="gpt-4o", api_key=get_api_key)
# Verify client was created successfully
assert client.model_id == "gpt-4o"
# OpenAI SDK now manages callable API keys internally
assert client.client is not None