Fixed: "Input should be a valid string" When pydantic Reads a Number from YAML
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.
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:
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:
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:
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:
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

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:
import yaml
with open("params.yaml", encoding="utf-8") as file:
value = yaml.safe_load(file)["data"]["sha256"]
print(repr(value), type(value).__name__)
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
atofand 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
strfield given anintraisesstring_type.
The Fix
Put the value in quotes in the YAML file, so YAML always reads it as text:
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:
'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:
'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:
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

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
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving a checksum, ID or code unquoted in YAML | It works until a value happens to be all digits, then breaks | Quote values that must stay text |
Turning on coerce_numbers_to_str | The error goes away, but 0 arrives instead of the 64 zeros | Quote the value in the YAML file |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Input should be a valid string [type=string_type, input_value=<number>, input_type=int] | YAML read the value as a number | Put the value in quotes in the YAML file |
The same error with input_type=float | The value looks like a decimal, such as 3.10 | Quote 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
- YAML 1.1 integer type — the forms PyYAML reads as numbers.
- pydantic validation errors: string_type — the error's reference.
- ConfigDict.coerce_numbers_to_str — what the setting does.
