Skip to content

fix: sanitize tool_calls for all strict APIs (Fireworks, Mistral, etc.)#5183

Merged
teknium1 merged 7 commits into
NousResearch:mainfrom
lumethegreat:fix/tool-call-sanitization-strict-apis
Apr 5, 2026
Merged

fix: sanitize tool_calls for all strict APIs (Fireworks, Mistral, etc.)#5183
teknium1 merged 7 commits into
NousResearch:mainfrom
lumethegreat:fix/tool-call-sanitization-strict-apis

Conversation

@lumethegreat

@lumethegreat lumethegreat commented Apr 5, 2026

Copy link
Copy Markdown

Fix: Tool Call Sanitization for Strict APIs

Problem

Codex-only fields (call_id, response_item_id) were being sent to strict APIs
like Fireworks, causing 400 errors:
"Extra inputs are not permitted, field: 'messages[X].tool_calls[Y].call_id'"

Root Cause

Sanitization was only applied for api.mistral.ai, not for other strict APIs.

Solution

Created _should_sanitize_tool_calls() method that returns True for all APIs
except codex_responses. Updated 3 locations in run_agent.py:

  1. flush_memories() - line ~5397
  2. _handle_max_iterations() - line ~6285
  3. run_conversation() - line ~6816

Tests Added

  • test_should_sanitize_tool_calls_codex_vs_chat
  • test_fireworks_compatible_messages_after_sanitization
  • test_codex_preserves_fields_for_replay
  • test_sanitize_method_with_fireworks_provider
  • test_no_sanitize_for_codex_responses

All existing tests pass:

  • 221 tests in test_run_agent.py ✓
  • 71 tests in test_provider_parity.py ✓

Commits

  1. feat: add _should_sanitize_tool_calls() method
  2. refactor: use _should_sanitize_tool_calls in flush_memories()
  3. refactor: use _should_sanitize_tool_calls in _handle_max_iterations()
  4. refactor: use _should_sanitize_tool_calls in run_conversation()
  5. test: add test for _should_sanitize_tool_calls()
  6. test: add strict API validation tests for Fireworks compatibility
  7. docs: update docstring to mention Fireworks strict validation

Lume added 7 commits April 5, 2026 07:32
Adds a centralized method to determine when tool_calls need sanitization
for strict APIs. Returns True for all APIs except codex_responses mode.
This prevents 400 errors from providers like Fireworks that reject unknown
fields (call_id, response_item_id) in tool_calls.
Replaces hardcoded Mistral check with the new _should_sanitize_tool_calls()
method. This ensures tool_calls are sanitized for all strict APIs, not
just Mistral. Prevents 400 errors from Fireworks and other providers.
Replaces hardcoded Mistral check with the new _should_sanitize_tool_calls()
method. Ensures summary generation works correctly with Fireworks and
other strict APIs that reject unknown tool_call fields.
Replaces hardcoded Mistral check with the new _should_sanitize_tool_calls()
method. Updates comment to mention Fireworks alongside Mistral as strict
APIs requiring tool_call field sanitization.
Adds test verifying that:
- Codex mode returns False (no sanitization needed)
- Chat completions mode returns True (sanitization needed)
- Anthropic mode returns True (sanitization needed)

This ensures strict APIs like Fireworks receive properly sanitized tool_calls.
Adds comprehensive tests verifying:
- Fireworks-compatible messages after sanitization
- Codex mode preserves fields for Responses API replay
- Fireworks provider triggers sanitization correctly
- Codex responses mode correctly skips sanitization

Prevents regression of 400 validation errors on strict APIs.
Updates _sanitize_tool_calls_for_strict_api docstring to explicitly
mention Fireworks alongside Mistral as strict APIs requiring sanitization.
Also documents the specific fields that are stripped (call_id, response_item_id).
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.

2 participants