Skip to main content

Fixed: "Input should be a valid string" When pydantic Reads a Number from YAML

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A settings file loaded with PyYAML and checked with pydantic stopped with Input should be a valid string [type=string_type, input_value=0, input_type=int], for a value that was clearly text in the file. YAML had read it as a number. Quoting the value in the YAML file fixed it.

What I Was Doing​

In a machine learning project, a settings file, params.yaml, holds the SHA-256 checksum of the dataset the project downloads. A pydantic model checks it as text: exactly 64 hexadecimal characters. The project's download command refuses any file whose checksum doesn't match.

src/bike_demand_forecast/params.py (excerpt)
class DataParams(Section):
url: str = Field(pattern=r"^https://")
sha256: str = Field(pattern=r"^[0-9a-f]{64}$")
file: str = Field(min_length=1)

I replaced the checksum in params.yaml with 64 zeros:

params.yaml (data section)
data:
url: https://archive.ics.uci.edu/static/public/275/bike+sharing+dataset.zip
sha256: 0000000000000000000000000000000000000000000000000000000000000000
file: hour.csv

Then I ran the download command:

Terminal
uv run download

What I Expected​

The download to start, and the command's own check to refuse the file, because its real checksum isn't 64 zeros:

Expected output (last line)
ERROR bike_demand_forecast.download: Checksum mismatch: expected 0000000000000000000000000000000000000000000000000000000000000000, got ...

What I Got​

The command never started the download. pydantic refused the settings file first, with a long traceback ending in:

Output (last lines)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Params
data.sha256
Input should be a valid string [type=string_type, input_value=0, input_type=int]
For further information visit https://errors.pydantic.dev/2.14/v/string_type

The last lines of the traceback: 1 validation error for Params, data.sha256, Input should be a valid string with input_value=0 and input_type=int.

input_value=0, input_type=int: pydantic had been given the whole number 0, not 64 characters of text.

What I Checked​

I loaded the file with PyYAML alone, the same way the project does, and printed the checksum's value and type:

peek_checksum.py
import yaml

with open("params.yaml", encoding="utf-8") as file:
value = yaml.safe_load(file)["data"]["sha256"]

print(repr(value), type(value).__name__)
Terminal
uv run python peek_checksum.py
Output
0 int

The terminal after uv run python peek_checksum.py: 0 int.

So the change happened in YAML, before pydantic saw the value.

Why It Happens​

Two rules meet here:

  • YAML reads digits as a number. PyYAML follows YAML 1.1, whose integer type says integers may "have leading “0” to signal an octal base". Sixty-four zeros match that form, so PyYAML returned the number 0. A real checksum almost always contains letters from a to f and stays text, which is why the file worked until now.
  • pydantic doesn't turn numbers into text. Its configuration reference says: "Pydantic doesn't allow number types (int, float, Decimal) to be coerced as type str by default." A str field given an int raises string_type.

The Fix​

Put the value in quotes in the YAML file, so YAML always reads it as text:

params.yaml (data section)
data:
url: https://archive.ics.uci.edu/static/public/275/bike+sharing+dataset.zip
sha256: "0000000000000000000000000000000000000000000000000000000000000000"
file: hour.csv

The same peek_checksum.py now printed the full value as a string:

Output
'0000000000000000000000000000000000000000000000000000000000000000' str

Quote any value that must stay text but could look like a number: checksums, IDs, codes with leading zeros, version numbers such as 3.10.

Why Not coerce_numbers_to_str?​

pydantic's ConfigDict(coerce_numbers_to_str=True) makes the error go away, but it can't bring the digits back. A model with that setting, given the 0 that YAML produced, stored:

Output
'0'

One character instead of 64 zeros: YAML had already thrown the rest away. The quotes fix the cause; the setting only hides it.

After the Fix​

With the quoted value, uv run download got past the settings and reached its own check, which refused the file as it should:

Output
INFO bike_demand_forecast.download: Downloading https://archive.ics.uci.edu/static/public/275/bike+sharing+dataset.zip
ERROR bike_demand_forecast.download: Checksum mismatch: expected 0000000000000000000000000000000000000000000000000000000000000000, got b70182d0d0508e9abbb79306ce5c0cec34869000f8220175ac83d11dbe845401

The terminal after the fix: the download started, and the command refused the file with Checksum mismatch, expected 64 zeros, got b70182d0... .

pydantic accepted the 64 characters, the download ran, and the checksum check did its job. With the real checksum back in place, in quotes, the download succeeds.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving a checksum, ID or code unquoted in YAMLIt works until a value happens to be all digits, then breaksQuote values that must stay text
Turning on coerce_numbers_to_strThe error goes away, but 0 arrives instead of the 64 zerosQuote the value in the YAML file

Troubleshooting​

SymptomLikely causeFix
Input should be a valid string [type=string_type, input_value=<number>, input_type=int]YAML read the value as a numberPut the value in quotes in the YAML file
The same error with input_type=floatThe value looks like a decimal, such as 3.10Quote it too

FAQ​

Why did it work with the real checksum?​

The real checksum, b70182d0…, contains letters, so YAML can't read it as a number and leaves it as text.

Should pydantic be strict instead?​

Strictness wouldn't help here: the problem is that the number arrived at all. The fix belongs in the YAML file.

Conclusion​

Input should be a valid string [type=string_type, input_value=0, input_type=int] meant that YAML had turned a value made of digits into a number before pydantic saw it. Quoting the value in params.yaml kept it as text, and the download command's own checksum check worked again.

Additional Resources​