fix(agent): strip finish_reason from assistant messages to fix Mistral 422 errors#253
Merged
teknium1 merged 2 commits intoNousResearch:mainfrom Mar 2, 2026
Merged
Conversation
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. |
Contributor
|
LGTM |
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.
This was referenced Mar 5, 2026
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.
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.
Problem
Fixes #134
When using Mistral API directly (
api.mistral.ai), the second message always fails with HTTP 422:Root Cause
In
_build_assistant_message(), afinish_reasonfield is stored on every assistant message dict for internal trajectory tracking. When these messages are replayed as conversation history in subsequent API calls, thefinish_reasonfield 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_reasonandreasoningfrom assistant messages before sending them to the API, in both the main agent loop and the memory flush loop:Testing
Verified that
finish_reasonis no longer present in outbound API message payloads.