Skip to content

Conversation

@ArthurConmy
Copy link

Note: 🤖 Generated with Claude Code -- but the problem did impact me

Problem

OpenRouter can return "tool_calls" as finish_reason when the model decides to use tools. The field_validator only handled "tool_use" (Anthropic format), causing a ValidationError:

ValidationError: 1 validation error for LLMResponse
stop_reason
  Value error, Invalid stop reason: tool_calls [type=value_error, input_value='tool_calls', input_type=str]

This is particularly bad because InferenceAPI is commonly used with asyncio.gather() for batch processing. An unhandled exception from one request kills the entire batch, potentially losing hours of work and all in-flight requests.

Fix

Map "tool_calls" to StopReason.TOOL_USE in the field_validator.

Context: Occurred during single-turn puzzle generation via OpenRouter with gpt-oss-120b, grok-4.1-fast, claude-haiku-4.5. No tools were passed in the request - the model unexpectedly returned tool_calls anyway.

OpenAI and OpenRouter return "tool_calls" as finish_reason when the model
decides to use tools. The field_validator only handled "tool_use" (Anthropic
format), causing a ValidationError:

```
ValidationError: 1 validation error for LLMResponse
stop_reason
  Value error, Invalid stop reason: tool_calls [type=value_error, input_value='tool_calls', input_type=str]
```

This is particularly bad because InferenceAPI is commonly used with
asyncio.gather() for batch processing. An unhandled exception from one
request kills the entire batch, potentially losing hours of work and all
in-flight requests.

Fix: Map "tool_calls" to StopReason.TOOL_USE in the field_validator.

Context: Occurred during single-turn puzzle generation via OpenRouter with
gpt-oss-120b, grok-4.1-fast, claude-haiku-4.5. No tools were passed in the
request - the model unexpectedly returned tool_calls anyway.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
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.

1 participant