Skip to content

fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors#253

Merged
teknium1 merged 2 commits into
NousResearch:mainfrom
Sertug17:fix/mistral-clean
Mar 2, 2026
Merged

fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors#253
teknium1 merged 2 commits into
NousResearch:mainfrom
Sertug17:fix/mistral-clean

Conversation

@Sertug17

@Sertug17 Sertug17 commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Problem

Fixes #134

When using Mistral API directly (api.mistral.ai), the second message always fails with HTTP 422:

'loc': ['body', 'messages', 2, 'assistant', 'finish_reason'],
'msg': 'Extra inputs are not permitted'

Root Cause

In _build_assistant_message(), a finish_reason field is stored on every assistant message dict for internal trajectory tracking. When these messages are replayed as conversation history in subsequent API calls, the finish_reason field is sent to the API.

Mistral's API strictly forbids extra fields in message objects and returns 422. The first message always works because there's no history yet — the error appears from the second message onward.

Fix

Strip finish_reason and reasoning from assistant messages before sending them to the API, in both the main agent loop and the memory flush loop:

if "reasoning" in api_msg:
    api_msg.pop("reasoning")
# Remove finish_reason - not accepted by strict APIs (e.g. Mistral)
if "finish_reason" in api_msg:
    api_msg.pop("finish_reason")

Testing

Verified that finish_reason is no longer present in outbound API message payloads.

@Sertug17 Sertug17 changed the title fix(agent): skip reasoning param for Mistral API to prevent 422 errors fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors Mar 1, 2026
@Sertug17

Sertug17 commented Mar 1, 2026

Copy link
Copy Markdown
Contributor Author

Note: While other PRs (#138, #250) address the finish_reason stripping issue, this PR also includes a fix for the reasoning parameter being sent to Mistral's API, which independently causes 422 errors. The reasoning field in extra_body is only valid for OpenRouter/Nous Portal endpoints — sending it directly to api.mistral.ai results in an "Extra inputs are not permitted" error regardless of the message history fix.
These two fixes together provide complete Mistral API compatibility.

@teknium1

teknium1 commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

LGTM

@teknium1 teknium1 merged commit 7a0b377 into NousResearch:main Mar 2, 2026
teknium1 added a commit that referenced this pull request Mar 5, 2026
The flush_memories() and run_conversation() code paths already stripped
finish_reason and reasoning from API messages (added in 7a0b377 via PR
#253), but _handle_max_iterations() was missed. It was sending raw
messages.copy() which could include finish_reason, causing 422 errors
on strict APIs like Mistral when the agent hit max iterations.

Now strips the same internal fields consistently across all three API
call sites.
unmodeled-tyler added a commit to unmodeled-tyler/hermes-agent that referenced this pull request Mar 10, 2026
…ompatibility

Mistral's API strictly validates the Chat Completions schema and rejects
unknown fields with 422 "Extra inputs are not permitted". The call_id and
response_item_id fields were added for Codex Responses API support (ce175d7)
but are not part of the Chat Completions spec.

This extends the fix from PR NousResearch#253 (which stripped finish_reason) to also
strip these internal fields before sending to strict APIs like Mistral.

Changes:
- Strip call_id and response_item_id from tool_calls in 3 locations:
  - Main conversation loop
  - _handle_max_iterations()
  - flush_memories()
- Fix 429 rate limit handling (was incorrectly treated as non-retryable)
- Update .gitignore with common patterns

The Codex Responses API code path has built-in fallback logic that uses
the 'id' field when call_id is not present, so this change is safe for
both API modes.
unmodeled-tyler added a commit to unmodeled-tyler/hermes-agent that referenced this pull request Mar 10, 2026
…ompatibility

Mistral's API strictly validates the Chat Completions schema and rejects
unknown fields with 422 "Extra inputs are not permitted". The call_id and
response_item_id fields were added for Codex Responses API support (ce175d7)
but are not part of the Chat Completions spec.

This extends the fix from PR NousResearch#253 (which stripped finish_reason) to also
strip these internal fields before sending to strict APIs like Mistral.

Changes:
- Strip call_id and response_item_id from tool_calls in 3 locations:
  - Main conversation loop
  - _handle_max_iterations()
  - flush_memories()
- Fix 429 rate limit handling (was incorrectly treated as non-retryable)
- Update .gitignore with common patterns

The Codex Responses API code path has built-in fallback logic that uses
the 'id' field when call_id is not present, so this change is safe for
both API modes.
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
…l 422 errors (NousResearch#253)

* fix(agent): skip reasoning param for Mistral API to prevent 422 errors

* fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
The flush_memories() and run_conversation() code paths already stripped
finish_reason and reasoning from API messages (added in 0456c8a via PR
NousResearch#253), but _handle_max_iterations() was missed. It was sending raw
messages.copy() which could include finish_reason, causing 422 errors
on strict APIs like Mistral when the agent hit max iterations.

Now strips the same internal fields consistently across all three API
call sites.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
The flush_memories() and run_conversation() code paths already stripped
finish_reason and reasoning from API messages (added in 7a0b377 via PR
NousResearch#253), but _handle_max_iterations() was missed. It was sending raw
messages.copy() which could include finish_reason, causing 422 errors
on strict APIs like Mistral when the agent hit max iterations.

Now strips the same internal fields consistently across all three API
call sites.
olympus-terminal pushed a commit to olympus-terminal/hermes-agent that referenced this pull request May 16, 2026
…l 422 errors (NousResearch#253)

* fix(agent): skip reasoning param for Mistral API to prevent 422 errors

* fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors
olympus-terminal pushed a commit to olympus-terminal/hermes-agent that referenced this pull request May 16, 2026
The flush_memories() and run_conversation() code paths already stripped
finish_reason and reasoning from API messages (added in 7efd4cf via PR
NousResearch#253), but _handle_max_iterations() was missed. It was sending raw
messages.copy() which could include finish_reason, causing 422 errors
on strict APIs like Mistral when the agent hit max iterations.

Now strips the same internal fields consistently across all three API
call sites.
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
…l 422 errors (NousResearch#253)

* fix(agent): skip reasoning param for Mistral API to prevent 422 errors

* fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
The flush_memories() and run_conversation() code paths already stripped
finish_reason and reasoning from API messages (added in 1878cdd via PR
NousResearch#253), but _handle_max_iterations() was missed. It was sending raw
messages.copy() which could include finish_reason, causing 422 errors
on strict APIs like Mistral when the agent hit max iterations.

Now strips the same internal fields consistently across all three API
call sites.
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.

Error code: 422 when calling mistral api

2 participants