Repository navigation
feat(py/openai): send Mistral's field names when base_url points at Mistral - #123
Merged
Merged
Conversation
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
reviewed
Nov 16, 2026
cabljac
left a comment
Contributor
There was a problem hiding this comment.
A few comments/questions
…, 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
marked this pull request as ready for review
November 17, 2026 10:35
cabljac
reviewed
Nov 17, 2026
cabljac
left a comment
Contributor
There was a problem hiding this comment.
A few comments/questions
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
approved these changes
Nov 19, 2026
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.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's new
The OpenAI plugin now works against Mistral, as its own
mistralplugin besideOpenAI():Mistral()(new) is the same asOpenAI(provider='mistral')(new argument): with nobase_urlit goes tohttps://api.mistral.ai/v1; with one, it sends Mistral's field names on any host, for a gateway in front of Mistral.mistral, so its actions aremistral/<id>as in the JS and Dart plugins, and it can be registered next toOpenAI().Mistral.model()builds refs.mistral-large-latest,mistral-large-4,mistral-small-latest,ministral-14b/8b/3b-latest,codestral-latest,mistral-medium-latest, and themistral-embed/codestral-embedembedders), not OpenAI's./modelsentry'scapabilities, as JS and Dart do: ids withembedare embedders,completion_chatentries are chat models with tool and image support fromfunction_callingandvision, and OCR, moderation, transcription and speech models are left out.random_seed,max_tokens,output_dimension), refusestop_kandlogprobs=True/top_logprobs, which Mistral's models reject (logprobs=Falseis 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.thinkingchunks 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 ownthinkingchunk with its own signature, as in the Go and JS plugins, and reasoning from other models in a mixed history is left out.reasoning_effort='max'for Mistral, and refuses non-image media (a PDF, say) before the request, since media goes out asimage_url.Breaking changes
Mistral(), a Mistralbase_url,OPENAI_BASE_URLpointing at Mistral, orprovider='mistral') reads its key fromapi_key=orMISTRAL_API_KEY, neverOPENAI_API_KEY, so an OpenAI key is never sent to Mistral. Anyone who pointedbase_urlat Mistral with the Mistral key inOPENAI_API_KEYmust move it toMISTRAL_API_KEY(or passapi_key=). Until then calls fail withFAILED_PRECONDITION, whose message saysOPENAI_API_KEYis 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 themistralplugin: its models move fromopenai/<id>tomistral/<id>, and it no longer registers OpenAI's models.Why
Mistral rejects request fields it doesn't define (422), so
seed, embedderdimensionsand OpenAI-only settings failed against it, and replies with thinking chunks failed to parse. Under theopenainame a Mistral app listed OpenAI's models and couldn't run beside the OpenAI plugin.Not doing here
MISTRAL_API_KEY. Checked by hand against api.mistral.ai: listing, chat, streaming with usage, reasoning (streamed and replayed), embeddings, andOpenAI()+Mistral()together.