Code Beautifier
Input should be a valid integer

Fix Pydantic "Input should be a valid integer"

Pydantic v2 rejected a value for an int field: a string like "42", a float like 18.25, or a null. What each case means and the annotation that fixes it.

Input that triggers it

{"id": "42", "score": 18.25, "rank": null}
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Style:
json
Pythonpython

What the error means

Pydantic v2 validates every field against its annotation and reports each failure with a location, a message, and the offending input. Input should be a valid integer is the message for an int field that received something it could not accept. The full report looks like score: Input should be a valid integer, got a number with a fractional part [type=int_from_float, input_value=18.25], and the type= tag tells you which of several cases you are in.

The example triggers three at once: "id" is a string that could be an integer (int_parsing succeeds in lax mode, so this one only fails in strict mode), "score" is a float with a fractional part (int_from_float), and "rank" is null (int_type).

Why it happens

  • A fractional number in an int field. Pydantic v2 accepts 18.0 for an int but rejects 18.25; v1 truncated it silently, which is why upgrades surface this.
  • Null in a non-Optional field. rank: int requires an integer; None is not one.
  • A string that is not numeric ("abc", ""), or any string at all under strict=True.
  • The model was generated from a sample where score happened to be whole and rank happened to be set.

How to fix it

  1. Paste a payload that shows the real range of values into the generator above. It emits float for a field with any fractional sample, Optional[int] = None for a field that is ever null, and str for a field that is ever a non-numeric string.
  2. In an existing model, change the annotation to match the data: score: float, rank: Optional[int] = None.
  3. If the producer sends numbers as strings and you want them coerced, leave the field as int and stay in lax mode (the default); "42" becomes 42. Use strict=True only where coercion would hide bugs.
  4. If a float should be truncated on purpose, say so with a field_validator rather than relying on old v1 behaviour.

The model the example produces:

from typing import Optional
from pydantic import BaseModel

class Root(BaseModel):
    id: int
    score: float
    rank: Optional[int] = None

If it still fails

  • Read the type= tag in the error: int_parsing, int_from_float, int_type, and int_too_big each point at a different fix.
  • The same data against a Go struct produces cannot unmarshal string into Go struct field; the strictness is the same, only the wording differs.
  • Validate the raw JSON with JSON Validator first if you are unsure whether the value is a number or a string on the wire — the quotes are easy to miss in a log line.

Related errors