Skip to content

feat(py/openai): send Mistral's field names when base_url points at Mistral - #123

Merged
adesinah merged 24 commits into
mainfrom
feat/ai-832-mistral-openai-compat
Nov 20, 2026
Merged

adesinah merged 24 commits into
mainfrom
feat/ai-832-mistral-openai-compat

Conversation

@adesinah

@adesinah adesinah commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What's new

The OpenAI plugin now works against Mistral, as its own mistral plugin beside OpenAI():

from genkit_openai import Mistral, OpenAI

ai = Genkit(plugins=[OpenAI(), Mistral()])  # Mistral key from MISTRAL_API_KEY
res = await ai.generate(model=Mistral.model('mistral-small-latest'), prompt='Plan a menu',
                        config={'seed': 42, 'reasoning_effort': 'high'})
  • Mistral() (new) is the same as OpenAI(provider='mistral') (new argument): with no base_url it goes to https://api.mistral.ai/v1; with one, it sends Mistral's field names on any host, for a gateway in front of Mistral.
  • A Mistral target is named mistral, so its actions are mistral/<id> as in the JS and Dart plugins, and it can be registered next to OpenAI(). Mistral.model() builds refs.
  • It registers Mistral's catalog at startup (mistral-large-latest, mistral-large-4, mistral-small-latest, ministral-14b/8b/3b-latest, codestral-latest, mistral-medium-latest, and the mistral-embed / codestral-embed embedders), not OpenAI's.
  • Model listing reads each /models entry's capabilities, as JS and Dart do: ids with embed are embedders, completion_chat entries are chat models with tool and image support from function_calling and vision, and OCR, moderation, transcription and speech models are left out.
  • Sends Mistral's field names (random_seed, max_tokens, output_dimension), refuses top_k and logprobs=True/top_logprobs, which Mistral's models reject (logprobs=False is left out), and leaves out OpenAI-only settings Mistral doesn't declare (verbosity, safety_identifier, store, logit_bias, prompt_cache_retention, web_search_options, modalities, audio), with a warning in the log.
  • Reads thinking chunks as reasoning parts, in the order they came among the text (streamed or not), and replays them on the next turn. Each Mistral reasoning part goes back as its own thinking chunk with its own signature, as in the Go and JS plugins, and reasoning from other models in a mixed history is left out.
  • Accepts reasoning_effort='max' for Mistral, and refuses non-image media (a PDF, say) before the request, since media goes out as image_url.
  • Requests to every other host are unchanged.

Breaking changes

  • A Mistral target (Mistral(), a Mistral base_url, OPENAI_BASE_URL pointing at Mistral, or provider='mistral') reads its key from api_key= or MISTRAL_API_KEY, never OPENAI_API_KEY, so an OpenAI key is never sent to Mistral. Anyone who pointed base_url at Mistral with the Mistral key in OPENAI_API_KEY must move it to MISTRAL_API_KEY (or pass api_key=). Until then calls fail with FAILED_PRECONDITION, whose message says OPENAI_API_KEY is set but isn't used for Mistral, and model listing returns nothing.
  • OpenAI(base_url='https://api.mistral.ai/v1') (or another Mistral host) is now the mistral plugin: its models move from openai/<id> to mistral/<id>, and it no longer registers OpenAI's models.

Why

Mistral rejects request fields it doesn't define (422), so seed, embedder dimensions and OpenAI-only settings failed against it, and replies with thinking chunks failed to parse. Under the openai name a Mistral app listed OpenAI's models and couldn't run beside the OpenAI plugin.

Not doing here

  • No live tests in CI, since they need MISTRAL_API_KEY. Checked by hand against api.mistral.ai: listing, chat, streaming with usage, reasoning (streamed and replayed), embeddings, and OpenAI() + Mistral() together.

Mistral's OpenAI-compatible API rejects any field its schema doesn't
declare. With base_url pointing at api.mistral.ai the plugin now:

- drops stream_options on streaming calls
- sends max_tokens instead of max_completion_tokens
- sends seed as random_seed
- sends an embedder's dimensions as output_dimension

Requests to any other host are unchanged.
…rd top_k

- Reply content that arrives as a list of chunks (Magistral) is split:
  thinking chunks become reasoning parts, text chunks the text, on the
  streamed and non-streamed paths. String content is unchanged.
- codestral.mistral.ai gets the same rewrite as api.mistral.ai.
- top_k goes out to Mistral, which declares it.
- Streaming no longer adds stream_options for Mistral only to drop it.
- The embedder makes one create() call with only the options that are set.
- raw no longer warns when the reply content is a list.
…S hosts

- On Mistral hosts, a model message's reasoning goes back on the next
  turn as one leading thinking chunk, with the signature the reply
  carried. Other hosts still drop reasoning.
- A thinking chunk's signature is kept on the reasoning part as
  metadata.thoughtSignature.
- finish_reason model_length maps to LENGTH.
- api.eu.mistral.ai and api.us.mistral.ai get the Mistral rewrite.
A live call shows every Mistral model rejects top_k ("top_k sampling is not
enabled for this model"). mistral_chat_body now raises INVALID_ARGUMENT for
it, so the turn fails naming the setting and nothing is sent. Its docstring
also gives the current reason stream_options is dropped: Mistral accepts it
now, but reports usage on the last chunk without it.

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments/questions

Comment thread packages/genkit-openai/README.md Resolved
Comment thread packages/genkit-openai/README.md Resolved
…, add provider='mistral'

- Reasoning parts read from Mistral's thinking chunks are now marked
  metadata['reasoningSource'] = 'mistral', and only those go back to
  Mistral. Reasoning another model left in a mixed history (Gemini or
  Claude, with their thoughtSignature) is dropped, as it is for OpenAI.
  The mark lives on the part, so it survives a JSON round trip.
- With a Mistral target and no api_key, the plugin key comes from
  MISTRAL_API_KEY instead of OPENAI_API_KEY, so an OpenAI key is never
  sent to Mistral. An explicit api_key still wins.
- seed, max_completion_tokens and stream_options in extra get the same
  renames as in config instead of a 422 from Mistral.
- OpenAI(provider='mistral') applies the Mistral rewrites on any host,
  for a gateway in front of Mistral.
- Tests for replaying onto a tool-call-only turn (no content), a
  reasoning-only turn, and list content on a non-Mistral host.
- README notes that JS and Dart register these models as mistral/...
…g chunk

Joining a turn's reasoning into one thinking chunk with the last signature
paired that signature with text it didn't sign. Each Mistral-tagged
reasoning part now goes back as its own thinking chunk, in order, with its
own signature; parts with neither text nor a signature are skipped, and
with none left no thinking chunk is sent. This matches the Go and JS
plugins.

On the response side, a reply with several thinking chunks now gives one
reasoning part per chunk, each with its own signature. A streamed reply's
final message joins the fragments of its thinking chunk back into one
part, so the per-part replay doesn't split a signed chunk.
@adesinah
adesinah marked this pull request as ready for review November 17, 2026 10:35

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments/questions

Comment thread packages/genkit-openai/README.md Resolved
Comment thread packages/genkit-openai/README.md Resolved
With provider='mistral' and no base_url, the key switched to MISTRAL_API_KEY
and the body took Mistral's field names, but the SDK still sent both to
api.openai.com. Default to https://api.mistral.ai/v1, as JS's and Dart's
mistral() do. An explicit base_url (argument or client_options) still wins;
OPENAI_BASE_URL is not read for provider='mistral'.
…ral's client a None key

A Mistral target reads MISTRAL_API_KEY, not OPENAI_API_KEY. Anyone who had
pointed base_url at Mistral with the key in OPENAI_API_KEY now gets
FAILED_PRECONDITION, so the error says OPENAI_API_KEY is set but not used
for Mistral when it is.

_runtime_client also raised nothing when MISTRAL_API_KEY was unset and
passed api_key=None, which makes the SDK read OPENAI_API_KEY. _has_plugin_key
keeps that unreachable today; raising there keeps it so after a refactor.
A stream's Mistral reasoning fragments were joined into one part per run,
so two signed thinking chunks came back as one part with the second
signature, and on replay the first chunk's text went back under a
signature that doesn't match it. A fragment carrying a signature now
closes its part, so the stream ends with one part per signed chunk, as a
non-streamed reply does.
Mistral's ReasoningEffort declares 'max' (JS's Mistral config takes it), but
OpenAIConfig stopped at 'xhigh', so it failed config validation. 'max' is
now a ReasoningEffort value; for any target other than Mistral it is refused
before the request, since OpenAI's API doesn't have it, with a pointer to
extra={'reasoning_effort': 'max'} for a host that does.
Every media part goes out as an image_url chunk, so a PDF reached Mistral
as a broken image and failed there. For a Mistral target, media whose type
(content_type, else a data: URL's header) isn't image/* now fails with
INVALID_ARGUMENT naming the type, as JS's and Dart's Mistral plugins do. A
remote URL with no known type is still sent as an image.
A Mistral reply can interleave thinking and text chunks. The streamed
final message put every reasoning part ahead of every text part, and the
full reply did the same. Both now keep the chunks' order; reasoning from
its own field (DeepSeek's reasoning_content) still comes first, as before.
Mistral's chat schema is closed, and OpenAIConfig fields it doesn't
declare went out as given and failed the call with a 422. Checked live:
verbosity, safety_identifier, store, logit_bias, prompt_cache_retention and
web_search_options are rejected. A Mistral body now keeps only the fields
Mistral's ChatCompletionRequest declares (plus the renamed ones), as Dart's
mistralChatBody drops user and verbosity, and logs a warning naming what it
left out. logprobs, top_logprobs, service_tier and the other fields Mistral
declares still go out; extra is still sent as written.
…ities

A Mistral target was the 'openai' plugin and registered OpenAI's catalog
(gpt-*, dall-e, tts, whisper, text-embedding-3), so an app couldn't use
OpenAI and Mistral together, and a Mistral app listed OpenAI models that all
failed. Its /models listing was read by OpenAI's name rules too, so
voxtral-mini-tts-latest became a TTS action and mistral-ocr and moderation
models became chat models.

- A Mistral target (provider='mistral', a Mistral base_url, or
  OPENAI_BASE_URL on a Mistral host) is now the 'mistral' plugin, with
  actions at mistral/<id> as in JS and Dart. New Mistral plugin, the same as
  OpenAI(provider='mistral'), with a Mistral.model() ref helper.
- init registers Mistral's catalog (the chat models and embedders JS and
  Dart register), not OpenAI's.
- list_actions reads each /models entry's capabilities: ids with embed are
  embedders, completion_chat entries are chat models with tools and media
  from function_calling and vision, the rest are skipped. resolve serves any
  id as a chat model or embedder, never OpenAI's image or audio endpoints.
Mistral's schema declares logprobs and top_logprobs, but every model
answers 'Logprobs are not enabled for this model' with a 400. As in JS,
logprobs=True or any top_logprobs now fails with INVALID_ARGUMENT before the
request, like top_k, and logprobs=False is left out of the body.
# Conflicts:
#	packages/genkit-openai/src/genkit_openai/_models/_model.py

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comments

An uncatalogued Mistral id, including one found via /models, got
output=['text'], so generate(output_format='json') with no schema went
out as response_format text. List json in the unknown-model supports so
it sends json_object, as OpenAIModel does for unlisted ids.
@adesinah
adesinah merged commit 8ee94df into main Nov 20, 2026
20 checks passed
@adesinah
adesinah deleted the feat/ai-832-mistral-openai-compat branch November 20, 2026 16:51
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 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.

2 participants