Expand description
Response healing system for SimpleAgents.
Implements BAML-inspired JSON parsing and type coercion to handle malformed LLM outputs.
§Architecture
The healing system consists of three main components:
-
Jsonish Parser: Three-phase parsing strategy that handles malformed JSON
- Strip & Fix: Remove markdown, fix commas, normalize quotes
- Standard Parse: Try serde_json (fast path)
- Lenient Parse: Character-by-character state machine
-
Coercion Engine: Type coercion with confidence scoring
- String → Number coercion
- Fuzzy field matching (case-insensitive, snake_case ↔ camelCase)
- Union resolution with best-match selection
- Default value injection
-
Streaming Parser: Incremental parsing for streaming responses
- Partial value extraction
- Progressive emission during streaming
- Annotation support (stream.not_null, stream.done)
§Example
use simple_agents_healing::parser::JsonishParser;
let malformed = r#"```json
{"name": "test", "age": 25,}
```"#;
let parser = JsonishParser::new();
let result = parser.parse(malformed).unwrap();
assert_eq!(result.value["name"], "test");
assert_eq!(result.value["age"], 25);
assert!(result.flags.iter().any(|f| matches!(f,
simple_agent_type::coercion::CoercionFlag::StrippedMarkdown)));§Transparency
All transformations are tracked via CoercionFlags and assigned confidence scores:
- 1.0: Perfect parse, no healing needed
- 0.9-0.99: Minor fixes (markdown, trailing commas)
- 0.7-0.89: Type coercion or fuzzy field matching
- 0.5-0.69: Multiple coercions or truncation
- <0.5: Significant healing required, review recommended
Re-exports§
pub use coercion::CoercionConfig;pub use coercion::CoercionEngine;pub use parser::JsonishParser;pub use parser::ParserConfig;pub use parser::ParserResult;pub use schema::Field;pub use schema::ObjectSchema;pub use schema::Schema;pub use schema::StreamAnnotation;pub use streaming::PartialExtractor;pub use streaming::StreamingParser;
Modules§
- coercion
- Type coercion engine for schema-aligned parsing.
- parser
- JSON-ish parser for handling malformed LLM outputs.
- prelude
- Prelude module for convenient imports.
- schema
- Schema definition system for type validation and coercion.
- streaming
- Streaming JSON parser for incremental LLM response parsing.
- string_
utils - String utility functions for case conversion and fuzzy matching.
Structs§
- Coercion
Result - Result of a coercion operation with transparency.
Enums§
- Coercion
Flag - Flag indicating a specific coercion/healing operation.
- Healing
Error - Healing and coercion errors.
Type Aliases§
- Result
- Result type alias using SimpleAgentsError.