Skip to content

Commit 2cb2533

Browse files
committed
Update OpenAI package version and enhance client creation error handling
1 parent 6eba26b commit 2cb2533

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed

pdm.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = [
66
{name = "B.T. Franklin", email = "[email protected]"},
77
]
88
dependencies = [
9-
"openai>=1.57.0",
9+
"openai>=2.1.0",
1010
"python-dotenv>=1.0.1",
1111
"click>=8.1.7",
1212
]

src/compendiumscribe/create_llm_clients.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
54
from dotenv import load_dotenv
65
from openai import OpenAI
76

@@ -25,7 +24,13 @@ def create_openai_client(*, timeout: int | None = None) -> OpenAI:
2524
client_kwargs: dict[str, object] = {"api_key": api_key}
2625
client_kwargs["timeout"] = timeout or DEFAULT_TIMEOUT_SECONDS
2726

28-
return OpenAI(**client_kwargs)
27+
client = OpenAI(**client_kwargs)
28+
if not hasattr(client, "responses"):
29+
raise RuntimeError(
30+
"Installed openai package does not expose the Responses API. "
31+
"Upgrade to a newer openai release (e.g. `pip install -U openai`)."
32+
)
33+
return client
2934

3035

3136
__all__ = ["create_openai_client", "MissingAPIKeyError"]

src/compendiumscribe/research_domain.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from typing import Any, Iterable
1010

1111
from openai import OpenAI
12-
1312
from .model import Compendium
1413

1514

@@ -272,7 +271,11 @@ def _execute_deep_research(
272271
return response
273272

274273

275-
def _await_completion(client: OpenAI, response: Any, config: ResearchConfig):
274+
def _await_completion(
275+
client: OpenAI,
276+
response: Any,
277+
config: ResearchConfig,
278+
):
276279
attempts = 0
277280
current = response
278281

tests/test_create_llm_clients.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
from types import SimpleNamespace
1+
from __future__ import annotations
22

33
import pytest
44

5-
from compendiumscribe.create_llm_clients import (
6-
MissingAPIKeyError,
7-
create_openai_client,
8-
)
5+
from compendiumscribe.create_llm_clients import create_openai_client
96

107

11-
class DummyOpenAI:
12-
def __init__(self, **kwargs):
13-
self.kwargs = kwargs
8+
def test_create_openai_client_requires_responses_support(monkeypatch):
9+
class DummyOpenAI:
10+
def __init__(self, **_kwargs):
11+
pass
1412

15-
16-
def test_create_openai_client_requires_api_key(monkeypatch):
17-
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
13+
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
1814
monkeypatch.setattr(
1915
"compendiumscribe.create_llm_clients.load_dotenv",
2016
lambda: None,
@@ -24,25 +20,36 @@ def test_create_openai_client_requires_api_key(monkeypatch):
2420
DummyOpenAI,
2521
)
2622

27-
with pytest.raises(MissingAPIKeyError):
23+
with pytest.raises(RuntimeError) as exc_info:
2824
create_openai_client()
2925

26+
assert "Responses API" in str(exc_info.value)
27+
3028

31-
def test_create_openai_client_uses_timeout(monkeypatch):
32-
monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
33-
captured = {}
29+
def test_create_openai_client_returns_native_client(monkeypatch):
30+
instances: list[object] = []
3431

35-
def fake_openai(**kwargs):
36-
captured.update(kwargs)
37-
return SimpleNamespace(**kwargs)
32+
class DummyResponses:
33+
pass
3834

35+
class DummyOpenAI:
36+
def __init__(self, **kwargs):
37+
self.kwargs = kwargs
38+
self.responses = DummyResponses()
39+
instances.append(self)
40+
41+
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
42+
monkeypatch.setattr(
43+
"compendiumscribe.create_llm_clients.load_dotenv",
44+
lambda: None,
45+
)
3946
monkeypatch.setattr(
4047
"compendiumscribe.create_llm_clients.OpenAI",
41-
fake_openai,
48+
DummyOpenAI,
4249
)
4350

4451
client = create_openai_client(timeout=123)
4552

46-
assert captured["api_key"] == "sk-test"
47-
assert captured["timeout"] == 123
48-
assert client.api_key == "sk-test"
53+
assert client is instances[0]
54+
assert isinstance(client.responses, DummyResponses)
55+
assert instances[0].kwargs["timeout"] == 123

0 commit comments

Comments
 (0)