Skip to content
Closed
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 packages/genkit-openai/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ classifiers = [
"Typing :: Typed",
"License :: OSI Approved :: Apache Software License",
]
dependencies = ["genkit>=0.14.0,<2", "openai>=1.60.0,<3", "strenum>=0.4.15; python_version < '3.11'"]
dependencies = ["genkit>=0.14.0,<2", "openai>=1.68.0,<4", "strenum>=0.4.15; python_version < '3.11'"]
description = "OpenAI-compatible models on Genkit"
keywords = [
"genkit",
Expand Down
12 changes: 6 additions & 6 deletions packages/genkit-openai/src/genkit_openai/_models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def _usage_from_completion(usage: CompletionUsage | None) -> GenerationUsage:
if usage is None:
return GenerationUsage()

extras = usage.model_extra or {}
prompt = usage.prompt_tokens_details or PromptTokensDetails()
completion = usage.completion_tokens_details or CompletionTokensDetails()

Expand All @@ -154,16 +153,17 @@ def _usage_from_completion(usage: CompletionUsage | None) -> GenerationUsage:
('acceptedPredictionTokens', completion.accepted_prediction_tokens),
('rejectedPredictionTokens', completion.rejected_prediction_tokens),
# xAI counts the live-search sources it consulted and breaks image
# tokens out of the prompt; neither is in OpenAI's usage shape.
('numSourcesUsed', extras.get('num_sources_used')),
('imageTokens', (prompt.model_extra or {}).get('image_tokens')),
# tokens out of the prompt. Read by name: a field the SDK does not
# declare is a pydantic extra, and openai 3.x declares image_tokens.
('numSourcesUsed', getattr(usage, 'num_sources_used', None)),
('imageTokens', getattr(prompt, 'image_tokens', None)),
):
count = _token_count(value)
if count is not None:
custom[name] = count
# Presence decides for the price a gateway charged, not the value: a
# free-tier request is priced at an explicit zero, which is an answer.
cost = extras.get('cost')
cost = getattr(usage, 'cost', None)
if isinstance(cost, (int, float)) and not isinstance(cost, bool):
custom['cost'] = float(cost)

Expand All @@ -175,7 +175,7 @@ def _usage_from_completion(usage: CompletionUsage | None) -> GenerationUsage:
# DeepSeek reports cache hits in a usage field of its own and sends no
# prompt_tokens_details at all.
cached_content_tokens=(
_token_count(prompt.cached_tokens) or _token_count(extras.get('prompt_cache_hit_tokens'))
_token_count(prompt.cached_tokens) or _token_count(getattr(usage, 'prompt_cache_hit_tokens', None))
),
custom=custom or None,
)
Expand Down
7 changes: 4 additions & 3 deletions packages/genkit-openai/src/genkit_openai/_models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
from genkit.model import ModelRequest, ToolRequest
from genkit.plugin_api import StatusName, provider_error

# The SDK wraps httpx failures in APITimeoutError / APIConnectionError only
# while sending. A connection that drops while the SSE stream is being read
# comes out of ``async for`` as a raw httpx error, so catch these too.
# Before openai 3.0 the SDK wraps httpx failures in APITimeoutError /
# APIConnectionError only while sending. A connection that drops while the
# SSE stream is being read comes out of ``async for`` as a raw httpx error,
# so catch these too. openai 3.x wraps stream-read failures itself.
STREAM_TRANSPORT_ERRORS: tuple[type[Exception], ...] = (
httpx.TimeoutException,
httpx.NetworkError,
Expand Down
7 changes: 5 additions & 2 deletions packages/genkit-openai/tests/openai_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
async def test_generate_classifies_a_stream_cut_mid_read(
sample_request: ModelRequest, drop: Exception, status: str
) -> None:
"""The SDK wraps httpx errors only while sending; a stream cut after the 200 still maps for Fallback."""
"""A stream cut after the 200 maps for Fallback, whether or not the SDK wrapped the httpx error."""
first = {
'id': 'chatcmpl-1',
'object': 'chat.completion.chunk',
Expand All @@ -1849,4 +1849,7 @@ def handler(request: httpx.Request) -> httpx.Response:

assert chunks == ['Grilled']
assert exc_info.value.status == status
assert exc_info.value.__cause__ is drop
# openai<3 surfaces the raw httpx error; openai 3.x wraps it in
# APITimeoutError / APIConnectionError first.
cause = exc_info.value.__cause__
assert cause is drop or (cause is not None and cause.__cause__ is drop)
4 changes: 2 additions & 2 deletions tests/version_bounds_guard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ def test_real_workspace_passes_consistency_and_extras_contract() -> None:
}
assert referenced_plugins == all_plugins

# genkit-openai bounds openai>=1.60.0,<3.
# genkit-openai bounds openai>=1.68.0,<4.
openai_project = consistency.load_pyproject(str(PY_DIR / 'packages' / 'genkit-openai' / 'pyproject.toml'))[
'project'
]
assert 'openai>=1.60.0,<3' in openai_project['dependencies']
assert 'openai>=1.68.0,<4' in openai_project['dependencies']


def test_bump_transition_0_12_0_to_0_13_0_rolls_floors_and_advances_minor_cap(tmp_path: Path) -> None:
Expand Down
Loading
Loading