Metadata-Version: 2.4
Name: parsantic
Version: 0.4.0
Summary: Schema-aligned parsing
Project-URL: Repository, https://github.com/elyase/parsantic
Project-URL: Issues, https://github.com/elyase/parsantic/issues
Author: Yaser Martinez Palenzuela
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE
Keywords: json,json-repair,llm,parsing,pydantic,structured-output
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: pydantic>=2.0.0
Requires-Dist: pyyaml>=6.0.1
Requires-Dist: regex>=2024.0.0
Provides-Extra: ai
Requires-Dist: pydantic-ai>=0.0.36; extra == 'ai'
Provides-Extra: vision
Requires-Dist: pillow>=10.0.0; extra == 'vision'
Requires-Dist: pymupdf>=1.24.0; extra == 'vision'
Description-Content-Type: text/markdown

# parsantic

[![CI](https://github.com/elyase/parsantic/actions/workflows/ci.yml/badge.svg)](https://github.com/elyase/parsantic/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/parsantic)](https://pypi.org/project/parsantic/)
[![Python](https://img.shields.io/pypi/pyversions/parsantic)](https://pypi.org/project/parsantic/)
[![License](https://img.shields.io/pypi/l/parsantic)](https://github.com/elyase/parsantic/blob/main/LICENSE)

The structured extraction toolkit: parse, stream, extract, update, patch,
and coerce LLM output — locally, deterministically, with one clean API.

## Install

```bash
uv add parsantic
```

For LLM extraction and update features (OpenAI, Anthropic, Gemini, etc.):

```bash
uv add "parsantic[ai]"
```

## What it does

LLM output is messy. Models wrap JSON in markdown, add trailing commas, use
wrong-case enum values, and return partial objects mid-stream. Most tools
deal with this by retrying the LLM call. `parsantic` fixes it locally in
one pass:

````python
from enum import Enum
from pydantic import BaseModel
from parsantic import parse

class Priority(str, Enum):
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    CRITICAL = "critical"

class Task(BaseModel):
    title: str
    priority: Priority
    days_left: int
    done: bool = False

# The LLM returned this mess:
llm_output = """
Sure! Here's the task you requested:

```json
{
    // Task details
    "Title": "Fix the login bug",
    "priority": "HIGH",
    "Days-Left": "3",
    "done": false,
}
```

Let me know if you need anything else!
"""

task = parse(llm_output, Task).value
# Task(title='Fix the login bug', priority=<Priority.HIGH: 'high'>, days_left=3, done=False)
````

One call. No retry. Markdown fences, comments, surrounding prose,
wrong-case keys, kebab-to-snake key normalization, enum coercion,
string-to-int coercion, trailing commas — all handled.

It works even when the JSON is still arriving. Feed tokens as they come and
get valid, typed partial objects back *while the LLM is still generating*:

```python
from parsantic import parse_stream

stream = parse_stream(Task)

stream.feed('{"title": "Stre')
print(stream.parse_partial().value)
# TaskPartial(title='Stre', priority=None, days_left=None, done=None)  ← partial but typed

stream.feed('aming task", "pr')
print(stream.parse_partial().value)
# TaskPartial(title='Streaming task', priority=None, days_left=None, done=None)

stream.feed('iority": "low", "days_left": 5}')
task = stream.finish().value
# Task(title='Streaming task', priority=<Priority.LOW: 'low'>, days_left=5, done=False)
```

Every call to `parse_partial()` returns a valid Pydantic object (a generated
`TaskPartial` with all-optional fields) with whatever values are available so far.
No waiting for the full response.

## Extract from text (requires `[ai]` extra)

Turn unstructured text into typed objects — with source grounding:

```python
from pydantic import BaseModel
from parsantic import extract

class Person(BaseModel):
    name: str
    role: str
    years_experience: int

result = extract(
    "Dr. Sarah Chen is a principal ML engineer at Anthropic (3 years).",
    Person,
    model="openai:gpt-4o-mini",
)
result.value
# Person(name='Sarah Chen', role='principal ML engineer', years_experience=3)

# Every extracted value is grounded back to the source text
result.evidence[0]
# FieldEvidence(path='/name', value_preview='Sarah Chen', char_interval=(4, 14), ...)
```

## Coerce tool arguments

LLM tool calls return raw dicts with wrong types and casing.
`coerce()` fixes them against your schema — no string parsing needed:

```python
from parsantic import coerce

# Raw dict from an LLM tool call
tool_args = {"title": "Deploy", "priority": "HIGH", "days_left": "2", "done": "true"}

task = coerce(tool_args, Task).value
# Task(title='Deploy', priority=<Priority.HIGH: 'high'>, days_left=2, done=True)
```

The coercion engine handles case-insensitive and accent-insensitive enum
matching, string-to-number conversion, key normalization, and more — each
tracked with a penalty score so the least-edited interpretation always wins.

## Update existing objects (requires `[ai]` extra)

Once you've extracted a large object, new information may arrive. Asking the
LLM to regenerate all 50 fields risks silently dropping data it wasn't
paying attention to. `update()` handles this — it asks the LLM to produce
only the changes as JSON Patch operations, applies them, and validates the
result:

```python
from pydantic import BaseModel
from parsantic import update

class User(BaseModel):
    name: str
    role: str
    skills: list[str]
    years_experience: int

profile = {
    "name": "Alex Chen",
    "role": "Software Engineer",
    "skills": ["Python", "TypeScript", "SQL"],
    "years_experience": 3,
}

result = update(
    existing=profile,
    instruction="Alex got promoted to Senior Engineer and picked up Rust.",
    target=User,
    model="openai:gpt-4o-mini",
)
result.value
# User(name='Alex Chen', role='Senior Software Engineer',
#      skills=['Python', 'TypeScript', 'SQL', 'Rust'], years_experience=5)
result.patches
# [JsonPatchOp(op='replace', path='/role', value='Senior Software Engineer'),
#  JsonPatchOp(op='replace', path='/years_experience', value=5),
#  JsonPatchOp(op='add', path='/skills/-', value='Rust')]
```

The original document is never mutated. Under the hood, `update()` prompts
the LLM for RFC 6902 patches, parses the messy response with `parse()`,
applies the patches with safety rails (`remove` disabled by default), and
validates the result with schema-aware coercion. If validation fails, it
automatically retries with the error context.

## Candidate scoring

When the input is ambiguous, `parsantic` generates multiple candidate
interpretations and picks the one requiring the fewest transformations:

```python
from parsantic import parse_debug

debug = parse_debug('{"title": "Review PR", "priority": "Critical", "days_left": 1}', Task)
for c in debug.candidates:
    print(f"  score={c.score}  flags={c.flags}")
# score=-1  flags=()                    ← direct JSON parse (failed validation)
# score=3   flags=('case_insensitive',) ← coerced "Critical" → Priority.CRITICAL
print(debug.value)
# Task(title='Review PR', priority=<Priority.CRITICAL: 'critical'>, days_left=0, done=False)
```

Every coercion is tagged with a flag and a cost. You can inspect exactly
what happened and why.

## Comparison with similar libraries

`parsantic` focuses on one thing: getting a valid typed object from messy
LLM text with the least effort and fewest LLM calls.

### Quick comparison

| | parsantic | BAML | trustcall | llguidance | LangExtract |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **Approach** | Fix output locally | Fix output locally | Patch via LLM retry | Prevent at token level | LLM extraction pipeline |
| **Handles invalid JSON text** | Yes (local repair) | Yes (local repair) | N/A (tool calling) | N/A (constrained decoding) | No (expects valid JSON/YAML; can strip fences) |
| **Streaming** | Typed partial objects (Partial model) | Typed partial objects (generated Partial types) | — | Token-level masks | — |
| **Updates** | JSON Patch (targeted) | — | JSON Patch (LLM-generated) | — | Re-run extraction |
| **Source grounding** | Char/token spans + alignment | — | — | — | Char spans + fuzzy alignment |
| **Schema** | Pydantic models | `.baml` DSL | Pydantic / functions / JSON Schema | JSON Schema (subset) / CFG / regex | Example-driven (own format) |
| **Candidate scoring** | Weighted flags, inspectable | Scoring heuristics (internal) | — | — | — |
| **Install** | `pip install` | `pip install` (+ BAML CLI / codegen) | `pip install` | `pip install` (Rust-backed) | `pip install` |
| **Extra LLM calls on validation failure** | 0 | 0 | Yes (patch retries; configurable) | 0 | 0 |

### Detailed comparison

Each tool takes a fundamentally different approach to the structured-output
problem. Here is how they differ in practice.

#### [BAML](https://github.com/BoundaryML/baml) — Schema-Aligned Parsing in Rust

BAML is the closest in philosophy: let the LLM generate freely, then fix the
output locally. Its Rust-based parser handles the same classes of breakage
(markdown fences, trailing commas, wrong-case keys, partial objects) and
applies schema-aware coercion with a cost function.

**Where BAML goes further:**
- Dedicated `.baml` DSL with multi-language code generation (Python, TS, Ruby, Go, etc.)
- VS Code playground for live prompt testing
- Compact prompt schema (BAML docs claim ~80 % fewer tokens than JSON Schema; varies by schema)
- `@check` / `@assert` validators on output fields
- Dynamic types via TypeBuilder for runtime schema changes
- Multi-modal support (images, audio as first-class prompt inputs)
- Retry policies with exponential backoff and fallback client chains

**Where parsantic goes further:**
- Pure Python — no DSL, no code generation step
- Native Pydantic models as the schema (no new language to learn)
- JSON Patch support for targeted updates without full regeneration
- Source grounding with character/token-level evidence alignment
- Transparent candidate scoring with inspectable flags and costs
- Multi-pass extraction with non-overlapping span merging

#### [trustcall](https://github.com/hinthornw/trustcall) — Patch-Based Retry via LangChain

trustcall wraps LLM tool-calling with automatic validation and repair. When
a tool call fails Pydantic validation, it asks the LLM to generate JSON Patch
operations to fix the error rather than regenerating the entire output.

**Where trustcall goes further:**
- Simultaneous updates and insertions in one pass (`enable_inserts=True`)
- Works with LangChain chat models that support tool calling (broad provider coverage)
- Supports Pydantic models, plain functions, JSON Schema, and LangChain tools as input
- Graph-based execution with parallel tool-call validation (LangGraph)
- Optional deletes + policies for existing docs (`enable_deletes`, `existing_schema_policy`)

**Where parsantic goes further:**
- Zero extra LLM calls — all repairs are deterministic and local
- Streaming partial objects while the LLM is still generating
- Schema-aware coercion (enum matching, type conversion) without LLM involvement
- Candidate scoring shows exactly what was changed and why
- Source grounding ties extracted values back to source text positions
- No LangChain / LangGraph dependency

#### [llguidance](https://github.com/guidance-ai/llguidance) — Constrained Decoding at the Token Level

llguidance takes the opposite approach: instead of fixing broken output, it
prevents invalid output from being generated. At each decoding step it
computes a bitmask of valid tokens and blocks everything else.

**Where llguidance goes further:**
- Guarantees output that conforms to the provided grammar — no post-processing needed
- Supports context-free grammars (Lark-like syntax) beyond JSON Schema
- Parametric grammars for combinatorial structures (permutations, unique lists)
- ~50 μs per token mask for a 128k tokenizer (highly optimized Rust; depends on grammar)
- Powers OpenAI Structured Outputs; integrated into vLLM, SGLang, llama.cpp, and Chromium

**Where parsantic goes further:**
- Works with any LLM API — no inference-engine access needed (though llguidance
  is also available transparently via OpenAI's Structured Outputs API)
- Handles output that is *already generated* (logs, cached responses, tool-call results)
- JSON Patch updates, streaming partial objects, source grounding
- Candidate scoring with transparent coercion flags
- Pure Python, no Rust compilation or special deployment
- Handles messy real-world output (markdown, comments, surrounding text) that constrained
  decoding never produces but APIs frequently return

#### [LangExtract](https://github.com/google/langextract) — Extraction Pipeline with Visualization

LangExtract (Google) is an extraction-focused pipeline. It chunks long
documents, runs few-shot prompting in parallel, and aligns results back to
source text with interactive HTML visualization.

**Where LangExtract goes further:**
- Interactive HTML visualization with hover tooltips and colored highlighting
- Native Vertex AI Batch API integration for cost-efficient large-scale extraction
- Provider plugin system; Gemini provider supports schema-constrained output
- Schema derived from examples (no separate schema definition needed)

**Where parsantic goes further:**
- Local JSON fixing and schema-aware coercion (LangExtract parses JSON/YAML but does not repair invalid JSON)
- Streaming partial objects during generation
- JSON Patch for targeted document updates
- Candidate scoring with inspectable coercion trace
- Pydantic models as the schema (type-safe, IDE-friendly)
- Works as a standalone parser without any LLM — useful for cached/logged responses

### When to use what

| Scenario | Recommended tool |
| :--- | :--- |
| Parse messy LLM output into Pydantic models, no extra LLM calls | **parsantic** |
| Apply small updates to existing objects without regeneration | **parsantic** (JSON Patch) or **trustcall** (LLM-assisted) |
| Need source-grounded evidence spans from extracted data | **parsantic** or **LangExtract** |
| Guaranteed valid structure via cloud API (no self-hosting) | **llguidance** (via OpenAI Structured Outputs) |
| Own the inference engine and want grammar-level control | **llguidance** |
| Want a full DSL with code generation, VS Code tooling, and multi-modal | **BAML** |
| Production LLM orchestration with retries and fallback chains | **BAML** |
| Complex nested schemas that fail standard tool calling | **BAML** (SAP parsing) or **trustcall** (patch retries) |
| Validate and repair multiple tool calls in parallel | **trustcall** |
| Large-scale batch extraction with Vertex AI | **LangExtract** |
| Streaming typed partial objects during generation | **parsantic** or **BAML** |

## Development

```bash
uv sync
make test        # 345 tests
make check       # lint + format
make fmt         # auto-fix
```

## License

Apache-2.0
