Skip to main content

Crate schema_coerce

Crate schema_coerce 

Source
Expand description

§schema-coerce

Coerce an LLM-emitted JSON value to a small field-schema.

LLMs return "42" instead of 42, "true" instead of true, or wrap the answer in { "result": {...} }. This crate gives you a lossy coerce(value, &schema) that applies the obvious fixes:

  • String containing an integer → integer
  • String containing a float → float
  • String "true" / "false" / "yes" / "no" → bool
  • Object containing a single key whose value matches the schema → unwrap (handles wrappers like { "result": {...} })
  • Missing field → fill from default

§Example

use schema_coerce::{coerce, Field, Type};
use serde_json::json;

let schema = vec![
    Field { name: "count", ty: Type::Int, default: None },
    Field { name: "ok", ty: Type::Bool, default: Some(json!(false)) },
];
let raw = json!({ "count": "42", "ok": "yes" });
let fixed = coerce(raw, &schema);
assert_eq!(fixed["count"], json!(42));
assert_eq!(fixed["ok"], json!(true));

Structs§

Field
One field in the schema.

Enums§

Type
Expected JSON type for a field.

Functions§

coerce
Coerce v to an object matching schema.