Skip to main content

getting structured outputs from LLMs

Project description

json_partial_py is a resilient JSON parsing library written in Rust that goes beyond the strict JSON specification. It’s designed to parse not only valid JSON but also "JSON‐like" input that may include common syntax errors, multiple JSON objects, or JSON embedded in markdown code blocks.

This is python bindings for json_partial library (Rust).

Usage:

pip install json_partial_py

Simple Example

 
from pydantic import BaseModel
from json_partial_py import to_json_string

# Define a simple Pydantic model that matches the expected JSON structure.
class Person(BaseModel):
    name: str
    age: int

# Example input: A malformed JSON wrapped in Markdown fences.
MALFORMED_JSON = r"""
```json
{"name": "Bob", "age": 25}
```
"""

json_str = to_json_string(MALFORMED_JSON)
person = Person.model_validate_json(json_str)
print(person)

Advanced Example

 
from typing import List
from pydantic import BaseModel
from json_partial_py import to_json_string

# Define nested models for the JSON structure.
class Coordinates(BaseModel):
    lat: float
    lng: float

class Address(BaseModel):
    street: str
    city: str
    country: str
    coordinates: Coordinates

class Hobby(BaseModel):
    name: str
    years_active: int
    proficiency: str

class Person(BaseModel):
    name: str
    age: int
    address: Address
    hobbies: List[Hobby]

# Example input: A complex nested JSON wrapped in Markdown fences.
MALFORMED_NESTED_JSON = r"""
```json
{
    "name": "Bob",
    "age": 42,
    "address": {
        "street": "789 Pine Rd",
        "city": "Metropolis",
        "country": "USA",
        "coordinates": {"lat": 40.7128, "lng": -74.0060}
    },
    "hobbies": [
        {"name": "Cooking", "years_active": 5, "proficiency": "intermediate"},
        {"name": "Cycling", "years_active": 10, "proficiency": "expert"}
    ]
}
```
"""

json_str = to_json_string(MALFORMED_NESTED_JSON)
person = Person.model_validate_json(json_str)
print(person)

Publishing

To compile to the desired target using Zig, execute the following command: maturin build --release --target aarch64-unknown-linux-gnu --zig maturin build --release --target x86_64-unknown-linux-gnu --zig

After building, move the files from the target/wheels directory to the dist/ folder.

Finally, use the command uv publish to publish the package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_partial_python-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (937.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

File details

Details for the file json_partial_python-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for json_partial_python-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 36c6974d31b70c72c931a0bc6c4da2691e8c14b2701aa688848528c63cd5abd8
MD5 3d3399a40d9c4078de213fa1a2e6f48d
BLAKE2b-256 bf5e938e3a379823261c16aec644585e17c4e2e74f5a68776ae85c79eca22949

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page