Skip to content

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

Closed
adesinah wants to merge 10 commits into
mainfrom
feat/ai-832-mistral-openai-compat
Closed

adesinah wants to merge 10 commits into
mainfrom
feat/ai-832-mistral-openai-compat

Conversation

@adesinah

Copy link
Copy Markdown
Contributor

What's new

The OpenAI plugin now works against Mistral when base_url points at a Mistral host:

ai = Genkit(plugins=[OpenAI(api_key=os.environ['MISTRAL_API_KEY'], base_url='https://api.mistral.ai/v1')])
res = await ai.generate(model='openai/mistral-small-latest', prompt='Plan a menu',
                        config={'seed': 42, 'reasoning_effort': 'high'})
  • Sends Mistral's field names: random_seed, max_tokens, output_dimension, and no stream_options.
  • Reads thinking chunks as reasoning parts and replays them on the next turn.
  • Requests to every other host are unchanged.

Why

Mistral rejects request fields it doesn't define (422), so streaming, seed and embedder dimensions all failed against it.

Not doing here

  • No named Mistral plugin: models stay openai/<id>, unlike the JS, Go and Dart providers.
  • No live Mistral calls. Tests run against a fake server that enforces Mistral's schema.
  • Streamed usage without stream_options still needs a live call to confirm.

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.
@adesinah
adesinah force-pushed the feat/ai-832-mistral-openai-compat branch from 0b8dfbe to 904466d Compare November 12, 2026 16:21
@adesinah adesinah closed this Nov 12, 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