Skip to content

fix(py/openai): allow openai 3.x - #163

Closed
huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-openai-cap
Closed

huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-openai-cap

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

genkit-openai capped openai<3, but openai 3.0 shipped 2026-08-12 (latest 3.55.1). Anyone already on openai 3.x couldn't install genkit-openai. This widens the range to openai>=1.68.0,<4; the plugin and its tests now work on 1.x, 2.x and 3.x.

# --- BEFORE: read xAI's image_tokens from pydantic's extras ---
(prompt.model_extra or {}).get('image_tokens')
# openai 2.x => 258    (undeclared field, so it lands in model_extra)
# openai 3.x => None   (now a declared field, so model_extra doesn't have it)

# --- AFTER ---
getattr(prompt, 'image_tokens', None)
# => 258 on every SDK version

Main Changes

  • Widened the cap to <4, raised the floor to 1.68: openai 3.0's breaking changes are mostly transport (httpx2 by default, OS trust store). The plugin builds its client through the SDK, so nothing changes for users who don't pass a custom http_client. The 1.68 floor matches fix(py): enforce dependency bounds policy and raise declared floors #154: _audio.py passes instructions, which arrived in 1.68.
  • Read provider usage fields by name, not from model_extra: Previously, image_tokens, num_sources_used, cost and prompt_cache_hit_tokens were read from pydantic's model_extra, which only holds fields the model doesn't declare. openai 3.x declares image_tokens, so it moved out of model_extra and imageTokens silently disappeared from usage.custom. getattr reads a declared field or an extra, so this works on every SDK version and survives future promotions.
  • Kept the raw-httpx catch for stream errors: openai 3.x wraps transport errors raised mid-stream in APITimeoutError / APIConnectionError; earlier versions let raw httpx errors escape async for. Both paths still map to UNAVAILABLE / DEADLINE_EXCEEDED; the test now accepts the extra __cause__ level.

Verification

  • pytest packages/genkit-openai packages/genkit-vertexai: 550 passed at openai 1.68.0 (floor), 2.54.0 (latest 2.x) and 3.55.1 (lock).
  • Full suite at the lock: 7900 passed, 44 skipped. pyright, pyrefly, ty, ruff and check_consistency.py clean.

Notes

  • 3.x users passing a legacy httpx.AsyncClient via client_options={'http_client': ...} still work at runtime, but the SDK calls that temporary and type checkers reject it. They should build it from httpx2.
  • audio.translations.create raises a DeprecationWarning on 3.x.
  • Conflicts with fix(py): enforce dependency bounds policy and raise declared floors #154 only on the openai spec line and uv.lock; both set the 1.68 floor.

Widen genkit-openai to openai>=1.68.0,<4 and lock 3.55.1.

- Read extra usage counts with getattr. openai 3.x declares
  PromptTokensDetails.image_tokens, so it left model_extra and
  imageTokens was dropped. getattr reads a declared field or a
  pydantic extra on every SDK in range.
- openai 3.x wraps stream-read transport errors in APITimeoutError /
  APIConnectionError; the stream-cut test now accepts either cause
  depth. Classification is unchanged.
@huangjeff5 huangjeff5 closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant