feat(python-compat): reproduce pydantic's lax float and bool validation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yujong Lee 2026-09-23 20:05:21 -07:00
parent f5f4856491
commit faf1e9be68
4 changed files with 835 additions and 3 deletions

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@
python scripts/generate_fixtures.py > generated/values.json
Each row records `repr`, `str`, `json.dumps` (or its error), `bool`, `int`, `float`, pydantic's
lax `int` validation, and `pickle.dumps` at every protocol. Conversions record the result's
lax `int`, `float`, and `bool` validation, and `pickle.dumps` at every protocol. Conversions record the result's
`repr` or the exception class raised. Run it with the repository environment, which has pydantic. `literal` says whether `ast.literal_eval(repr(value))` gives the value back,
which is how Python reads `str(dict)` text back from a cache; the Rust tests reach the other
rows only through pickle. `view` is `repr` of the value as `pickle::loads` decodes it, with
@ -20,6 +20,8 @@ import warnings
from pydantic import TypeAdapter, ValidationError
LAX_INT = TypeAdapter(int)
LAX_FLOAT = TypeAdapter(float)
LAX_BOOL = TypeAdapter(bool)
# Entries are source texts, or `(name, source)` when the source is too long to read in a
# test report. `name` is what the Rust `KNOWN` table keys on.
@ -97,6 +99,11 @@ CORPUS = [
"'3.00'",
"'3.5'",
"' 0.5 '",
"'true'",
"' Yes '",
"'off'",
"'2'",
"1",
"'.5'",
"'5.'",
"'1e3'",
@ -369,6 +376,8 @@ def row(entry):
"int": converted(int, value),
"float": converted(float, value),
"lax_int": converted(LAX_INT.validate_python, value),
"lax_float": converted(LAX_FLOAT.validate_python, value),
"lax_bool": converted(LAX_BOOL.validate_python, value),
}
try:
entry["json"] = json.dumps(value)

View file

@ -1,10 +1,10 @@
//! pydantic's lax-mode validation, which LiteLLM's usage and response models apply to the
//! pydantic's lax-mode validation of `int`, `float`, and `bool`, which LiteLLM's usage and response models apply to the
//! numbers providers send.
use num_bigint::BigInt;
use num_traits::Zero;
use crate::number::{int_from_float, parse_int_literal};
use crate::number::{self, int_from_float, parse_int_literal};
use crate::{Error, PythonException, Value};
/// pydantic-core converts floats through `i64`, so larger magnitudes fail validation.
@ -52,3 +52,34 @@ pub fn lax_int(value: &Value) -> Result<BigInt, Error> {
_ => Err(invalid("Input should be a valid integer")),
}
}
/// `TypeAdapter(float).validate_python(value)` in lax mode.
pub fn lax_float(value: &Value) -> Result<f64, Error> {
match value {
Value::Bool(_) | Value::Int(_) | Value::Float(_) => number::float(value),
Value::Str(text) => number::float(&Value::Str(text.trim().to_owned())).map_err(|_| {
invalid("Input should be a valid number, unable to parse string as a number")
}),
Value::Bytes(bytes) => lax_float(&Value::Str(String::from_utf8_lossy(bytes).into_owned())),
_ => Err(invalid("Input should be a valid number")),
}
}
/// `TypeAdapter(bool).validate_python(value)` in lax mode.
pub fn lax_bool(value: &Value) -> Result<bool, Error> {
let unparsable = || invalid("Input should be a valid boolean, unable to interpret input");
match value {
Value::Bool(flag) => Ok(*flag),
Value::Int(number) if number.is_zero() => Ok(false),
Value::Int(number) if *number == BigInt::from(1) => Ok(true),
Value::Float(number) if *number == 0.0 => Ok(false),
Value::Float(number) if *number == 1.0 => Ok(true),
Value::Str(text) => match text.to_ascii_lowercase().as_str() {
"0" | "off" | "f" | "false" | "n" | "no" => Ok(false),
"1" | "on" | "t" | "true" | "y" | "yes" => Ok(true),
_ => Err(unparsable()),
},
Value::Bytes(bytes) => lax_bool(&Value::Str(String::from_utf8_lossy(bytes).into_owned())),
_ => Err(unparsable()),
}
}

View file

@ -64,6 +64,8 @@ struct Row {
int: Conversion,
float: Conversion,
lax_int: Conversion,
lax_float: Conversion,
lax_bool: Conversion,
json: Option<String>,
json_error: Option<String>,
pickle: Option<BTreeMap<String, String>>,
@ -236,6 +238,20 @@ fn check_value(mismatches: &mut Mismatches, row: &Row, value: &Value) {
&row.lax_int.expected(),
&outcome(pydantic::lax_int(value), |int| int.to_string()),
);
mismatches.check(
row,
"pydantic lax float",
&row.lax_float.expected(),
&outcome(pydantic::lax_float(value), float_repr),
);
mismatches.check(
row,
"pydantic lax bool",
&row.lax_bool.expected(),
&outcome(pydantic::lax_bool(value), |flag| {
if flag { "True" } else { "False" }.to_owned()
}),
);
let expected = row.json.clone().or_else(|| {
row.json_error
.clone()