# ValidationError: 1 validation error for ToolCall
value
  Input should be a valid integer [type=int_parsing, input_value='42.5', input_type=str]

- **ID:** `llm/tool-argument-type-coercion-failure`
- **Domain:** llm
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

When using tool calling with structured outputs, the LLM may generate arguments as strings (e.g., '42.5') instead of the expected type (e.g., integer), causing Pydantic or JSON schema validation to fail during type coercion.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| pydantic==2.5.0 | active | — | — |
| pydantic==2.7.0 | active | — | — |
| openai==1.16.0 | active | — | — |
| gpt-4-turbo-2024-04-09 | active | — | — |

## Workarounds

1. **Use Pydantic's `BeforeValidator` to coerce types: `from pydantic import BeforeValidator; def coerce_int(v): return int(float(v)) if isinstance(v, str) else v; class ToolCall(BaseModel): value: Annotated[int, BeforeValidator(coerce_int)]`** (90% success)
   ```
   Use Pydantic's `BeforeValidator` to coerce types: `from pydantic import BeforeValidator; def coerce_int(v): return int(float(v)) if isinstance(v, str) else v; class ToolCall(BaseModel): value: Annotated[int, BeforeValidator(coerce_int)]`
   ```
2. **In the tool schema, specify `type: 'number'` instead of `type: 'integer'` to accept both integers and floats as strings.** (80% success)
   ```
   In the tool schema, specify `type: 'number'` instead of `type: 'integer'` to accept both integers and floats as strings.
   ```
3. **Add a retry loop with a system prompt instructing the LLM to produce valid JSON with correct types: 'Ensure all numeric fields are numbers, not strings.'** (75% success)
   ```
   Add a retry loop with a system prompt instructing the LLM to produce valid JSON with correct types: 'Ensure all numeric fields are numbers, not strings.'
   ```

## Dead Ends

- **** — This may still fail if the string cannot be parsed as the target type (e.g., 'abc' for int) or produces unexpected values. (60% fail)
- **** — This can mask genuine errors like out-of-range values or non-numeric strings. (55% fail)
