Skip to content

feat(py): add OpenAICompatible for any OpenAI-compatible server, matching JS and Go - #187

Closed
huangjeff5 wants to merge 1 commit into
mainfrom
feat/py-openai-compatible
Closed

huangjeff5 wants to merge 1 commit into
mainfrom
feat/py-openai-compatible

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Related PRs: #151 (Anthropic(backend=GoogleCloud())), #187 → #188 (OpenAICompatible + backend=GoogleCloud()), then #185 (deprecate genkit-vertexai, merge last after #151, #187, and #188).

OpenAICompatible(name=..., base_url=..., api_key=...) connects any server that speaks the OpenAI API (OpenRouter, Together, Groq, vLLM, LiteLLM, a local model server) under a name you pick. It's the same shape as JS's openAICompatible({ name, baseURL, apiKey }) and Go's compat_oai.OpenAICompatible{Provider, BaseURL, APIKey}.

Until now, OpenAI(base_url=...) was the only way to reach those servers, and the OpenAI-specific plugin leaked into every one:

  • the Dev UI showed 42 OpenAI models that don't exist there;
  • OpenAI.gpt('openai/gpt-4o') stripped both prefixes and sent gpt-4o to OpenRouter;
  • OpenAI-only filters hid valid models (google/gemini-2.5-pro was dropped for its -pro suffix);
  • a server without /models broke the Dev UI list;
  • only one could run per app, because everything registered as openai/.
from genkit import Genkit
from genkit_openai import OpenAI, OpenAICompatible

# 1. Name the server; its models become openrouter/<id>
openrouter = OpenAICompatible(name='openrouter', base_url='https://openrouter.ai/api/v1', api_key=key)

# 2. Run it beside OpenAI's own API
ai = Genkit(plugins=[OpenAI(), openrouter])

# 3. The server's own ids are sent exactly as written, slashes and all
res = await ai.generate(model=openrouter.model('openai/gpt-4o'), prompt='Name a bakery.')
print(res.text)
# => Rise & Shine Bakehouse

# 4. A local server that needs no key; OPENAI_API_KEY is never read
local = OpenAICompatible(name='local', base_url='http://localhost:8000/v1')

Changes

  • New export OpenAICompatible. name and base_url are required, and models and embedders register as <name>/<id>.
    • <id> goes to the server exactly as written, with no prefix stripping and no guessing the model kind from its name.
    • ai.generate resolves chat models and ai.embed resolves embedders, by kind.
    • Each instance has its own name, so several instances and an OpenAI() coexist.
  • model(name, config=...) on the instance returns an OpenAIConfig ref (openrouter.model('meta-llama/...')). It's an instance method because the namespace belongs to the instance.
  • No built-in catalog. init() registers nothing. The Dev UI lists what the server's /models returns: ids containing embed as embedders, everything else as chat models. A server without /models, or one that rejects the call, lists nothing and logs a warning. The failure isn't cached, so the next listing tries again.
  • api_key takes a string, an async function, or None.
    • An async function is awaited by the SDK before each request, for tokens that expire.
    • None sends a placeholder, for servers that need no key. OPENAI_API_KEY is never read, so an OpenAI key can't reach another server.
    • A context.secrets key replaces the plugin key for that call.
  • Embedding is shared with OpenAI. The embed body moved into one helper (embed_texts) that both plugins call. A compatible server on Mistral's own host still gets Mistral's output_dimension field.
  • openai floor raised from >=1.60.0 to >=1.106.0. 1.106 is the first release whose api_key accepts an async function. 1.105 accepts only str | None.
  • README: a new "Any OpenAI-compatible server" section. OpenAI(base_url=...) is now described as a proxy or gateway in front of OpenAI, pointing to OpenAICompatible for anything else.

Decisions

  • A separate class, not OpenAI(name=...): OpenAI is the OpenAI-specific plugin, with OpenAI's catalog, name-based routing to image/TTS/STT, and OpenAI-only filters. Adding passthrough to it would mean "if base_url is set, skip the catalog and filters" branches throughout. A separate class has none of that by construction, and it matches the JS and Go split, where openAI() is a wrapper with the catalog on top of openAICompatible.
  • name is required: The name is the namespace. Defaulting it to openai would bring back the one-per-app collision and the openai/openai/... ambiguity. JS requires name, and Go requires Provider.
  • base_url is required: Without it, the plugin would quietly target OpenAI's API, which is what OpenAI() is for. No environment variable is read for either field, so what the constructor says is where calls go.
  • Chat and embeddings only: Image, TTS and STT are OpenAI endpoints with OpenAI-specific request shapes, and compatible servers rarely serve them. JS's generic plugin resolves chat models only by default.

Verification

  • packages/genkit-openai: 620 passed (602 existing, 18 new in openai_compatible_test.py). The new tests go through ai.generate and ai.embed against a fake HTTP server. They pin:
    • ids sent as written, including openai/-prefixed and -pro ids, by both string and typed ref;
    • coexistence with OpenAI() and a second instance;
    • an empty init();
    • /models listing, its caching, and the no-/models fallback;
    • OPENAI_API_KEY never sent;
    • the async key awaited per request;
    • context.secrets replacing the plugin key;
    • embeddings, including Mistral's field name;
    • constructor errors.
  • Mutation pass: I deleted or inverted each new branch in _compatible.py, one at a time (14 mutants), and reran the suite. Every mutant fails a test.
  • Other packages: packages/genkit-vertexai 53 passed, and tests/version_bounds_guard_test.py 30 passed.
  • Repo checks: scripts/check_consistency.py and uv lock --check pass.
  • Static checks:
    • ruff, pyright and ty pass on packages/genkit-openai.
    • pyrefly reports 1 error, in _models/_utils.py:476. That file is unchanged, and the error is the same on main.

Breaking changes

None to the API. Installs now need openai>=1.106.0. OpenAI(base_url=...) behaves the same; only its docs changed.

Follow-up

#188 (OpenAICompatible(backend=GoogleCloud(...))) is stacked on this PR, and #185 deprecates genkit-vertexai once #151, #187, and #188 land.

…hing JS and Go

OpenAICompatible(name=..., base_url=..., api_key=...) registers a server's
models and embedders as <name>/<id> and sends <id> as written. No built-in
catalog or OpenAI-only filters; the Dev UI lists /models and survives a server
without it. api_key takes a string, an async function, or None (never reads
OPENAI_API_KEY). OpenAI and OpenAICompatible share one embed helper. The
openai floor moves to 1.106.0, the first release with async api_key.
@huangjeff5

Copy link
Copy Markdown
Collaborator Author

Superseded by #196 (re-created to place into the single linear stack #151 → #196 → #197 → #198).

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