Skip to content

fix: remap max_completion_tokens to max_tokens for OpenAI-compatible providers#7765

Merged
jamadeo merged 2 commits intoblock:mainfrom
fresh3nough:fix/mistral-max-completion-tokens-7762
Mar 11, 2026
Merged

fix: remap max_completion_tokens to max_tokens for OpenAI-compatible providers#7765
jamadeo merged 2 commits intoblock:mainfrom
fresh3nough:fix/mistral-max-completion-tokens-7762

Conversation

@fresh3nough
Copy link
Copy Markdown
Contributor

Summary

When using OpenAI-compatible providers like Mistral via the declarative provider system, the OpenAI-specific parameter max_completion_tokens (used for O-series models) is not recognized and causes 422 errors.

Root Cause

OpenAiProvider is used by both native OpenAI and declarative OpenAI-compatible providers (Mistral, Groq, etc.). The create_request() function sets max_completion_tokens for O-series models, but this parameter is not recognized by non-OpenAI APIs like Mistral, which expect max_tokens instead.

Fix

Added sanitize_request_for_compat() method to OpenAiProvider that remaps max_completion_tokens to max_tokens for any non-native OpenAI provider (name != "openai"). Applied in the stream code path (the active chat completions path). If max_tokens already exists, the existing value is preserved.

Validation

  • 4 unit tests added and passing
  • cargo fmt clean
  • cargo clippy clean

Closes #7762

…providers

When using OpenAI-compatible providers like Mistral via the declarative
provider system, the OpenAI-specific parameter max_completion_tokens
(used for O-series models) is not recognized and causes 422 errors.

This adds a sanitize_request_for_compat method to OpenAiProvider that
remaps max_completion_tokens to max_tokens for any non-native OpenAI
provider, ensuring compatibility with Mistral and other OpenAI-compatible
APIs.

Closes block#7762

Signed-off-by: fre <[email protected]>
Copy link
Copy Markdown
Collaborator

@jamadeo jamadeo left a comment

Choose a reason for hiding this comment

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

True for mistral but always true for others? Maybe safer for now to check explicitly for mistral?

I'm looking at pi's implementation here and thinking we're missing a fair bit of openai compatibility nuances.

I wonder if the declarative provider configs just need more of these options

@jamadeo jamadeo assigned jamadeo and unassigned jh-block Mar 10, 2026
@fresh3nough
Copy link
Copy Markdown
Contributor Author

fresh3nough commented Mar 11, 2026

Good call — narrowed it to Mistral/Groq for safety in this PR. Long-term I think we should move these mappings into the declarative config

Address review feedback: instead of remapping max_completion_tokens
to max_tokens for all non-OpenAI providers, use an explicit allowlist
of providers known to reject max_completion_tokens. This prevents
silently breaking future providers that may support the field natively.

Signed-off-by: fre <[email protected]>
@jamadeo jamadeo added this pull request to the merge queue Mar 11, 2026
Merged via the queue into block:main with commit 3016fdd Mar 11, 2026
20 checks passed
lifeizhou-ap added a commit that referenced this pull request Mar 12, 2026
* main: (270 commits)
  test(acp): align provider and server test parity (#7822)
  fix(acp): register MCP extensions when resuming a session (#7806)
  fix(goose): load .gitignore in prompt_manager for hint file filtering (#7795)
  fix: remap max_completion_tokens to max_tokens for OpenAI-compatible providers (#7765)
  fix(openai): preserve Responses API tool call/output linkage (#7759)
  chore(deps): bump @hono/node-server from 1.19.9 to 1.19.11 in /evals/open-model-gym/mcp-harness (#7687)
  fix: return ContextLengthExceeded when prompt exceeds effective KV cache size (#7815)
  feat: MCP Roots support (#7790)
  fix(google): use `includeThoughts/part.thought` for thinking handling (#7593)
  refactor: simplify tokenizer initialization — remove unnecessary Result wrapper (#7744)
  Fix model selector showing wrong model in tabs (#7784)
  Stop collecting goosed stderr after startup (#7814)
  fix: avoid word splitting by space for windows shell commands (#7781) (#7810)
  Simplify and make it not break on linux (#7813)
  Add preferred microphone selection  (#7805)
  Remove dependency on posthog-rs (#7811)
  feat: load hints in nested subdirs (#7772)
  feat(acp): add read tool and delegate filesystem I/O to ACP clients (#7668)
  Support secret interpolation in streamable HTTP extension URLs (#7782)
  More logging for command injection classifier model training (#7779)
  ...
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.

Request fails with 422: max_completion_tokens not permitted when using Mistral provider

3 participants