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_python
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 using the following command:
cp target/wheels/* dist/
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_partial_python-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: json_partial_python-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 937.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73d5b5648c191f89f9fb9470b275a1342e7c6f1a71198400916883e1bf7a2e91
|
|
| MD5 |
4c44b6275a67d3dba9efa44840aca97e
|
|
| BLAKE2b-256 |
6dfc77b26ae7836ee9c22451288a8976d0012d46ab90d29d2e88205d4503efdc
|