Skip to content

fix(agent): strip finish_reason and internal fields before API calls#250

Closed
Bartok9 wants to merge 1 commit into
NousResearch:mainfrom
Bartok9:fix/mistral-extra-fields-134
Closed

fix(agent): strip finish_reason and internal fields before API calls#250
Bartok9 wants to merge 1 commit into
NousResearch:mainfrom
Bartok9:fix/mistral-extra-fields-134

Conversation

@Bartok9

@Bartok9 Bartok9 commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #134

Mistral API returns 422 error on the second message because internal fields like finish_reason are being sent in the request.

Error

Error code: 422 - {'detail': [{'type': 'extra_forbidden', 
'loc': ['body', 'messages', 2, 'assistant', 'finish_reason'], 
'msg': 'Extra inputs are not permitted', 'input': 'stop'}]}

Root Cause

finish_reason is response metadata from the API, not a valid request field. The code was storing it in messages for trajectory/debugging purposes, but then copying those messages (including the field) when making subsequent API calls.

Fix

Strip internal-only fields (finish_reason, reasoning) from message copies before sending to the API. Applied to all three places where API messages are constructed:

  1. flush_memories() API call
  2. Summary generation API call
  3. Main conversation API call

Fields Affected

  • finish_reason - Now stripped (caused the Mistral 422)
  • reasoning - Was already being stripped (for different reasons)

Testing

  • Verified the fix applies to all API call sites
  • Internal fields are still preserved in local messages for debugging/trajectory storage

Fixes NousResearch#134

Mistral API (and potentially others) returns 422 error when extra fields
like finish_reason are sent in the request. The error message is:

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

This fix ensures internal-only fields (finish_reason, reasoning) are
stripped from all message copies before sending to the API:
- flush_memories API call
- summary generation API call
- main conversation API call

The fields are preserved in the local messages array for trajectory
storage and debugging, but excluded from API requests.
@Bartok9 Bartok9 force-pushed the fix/mistral-extra-fields-134 branch from 48e46b6 to 3eb2937 Compare March 3, 2026 08:35
@teknium1

teknium1 commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for the report, @Bartok9! This issue was already largely fixed by PR #253 (commit 7a0b377 by @Sertug17), which stripped finish_reason from API messages in both flush_memories() and run_conversation().

Your PR did catch one remaining gap though — _handle_max_iterations() was still doing a bare messages.copy() without stripping internal fields. We've patched that in commit 41adca4, which now strips finish_reason and reasoning consistently across all three API call sites.

Closing this since the fix is now fully covered, but appreciate you spotting the missing code path!

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