Skip to main content

agentic_json

Hey there. If you build AI applications, you probably know the pain of working with structured JSON outputs from Large Language Models.

Normally, AI programs ask the model for a JSON object and then just sit there. You have to wait for the entire JSON string to finish generating before you can call json.loads(), update your UI, or trigger background computations. If the model is generating a long response or a detailed conversational reply, your users are left staring at a loading spinner.

I built agentic_json to fix that.

This library provides a real-time stream parser designed specifically for LLM outputs. Instead of waiting for the full payload, it parses the JSON stream chunk by chunk. You can trigger events the exact millisecond a specific key-value pair completes, or even stream the text of a specific value as it generates.

Because LLMs can be unpredictable, the library also comes with built-in fail-safes. You define a schema with expected types and default values. If the AI hallucinates malformed content or outputs a string when you wanted an integer, agentic_json catches the mismatch and falls back to your safe default. This drastically reduces mismatch risks and keeps your application from crashing.

Key Features

  • Real-Time Key Extraction: Fire callbacks the moment a key is fully generated, long before the rest of the JSON object finishes.

  • Value Streaming: Hook into specific text fields to stream characters directly to your UI.

  • Fail-Safe Schema: Define strict types and fallback defaults. If the AI breaks the JSON formatting, your app survives.

  • Seamless Async Integration: Built from the ground up to work with modern Python async workflows.

Quick Start

Here is a look at how you can use agentic_json to build an interactive AI interviewer. In this example, we stream the AI's response and trigger different actions based on the keys being generated.

Install it using pip install agentic-json

import asyncio
from agentic_json import StreamEngine, Field
from ollama import AsyncClient

# Defining your strict schema with fail-safe defaults
schema = {
    "question_number": Field(type=int, default=0),
    "type": Field(type=str, default="General"),
    "end": Field(type=bool, default=False),
    "text": Field(type=str, default=""),
}

engine = StreamEngine(schema=schema)

# Set up your real-time callbacks

@engine.on_complete("question_number") # Triggers after a key-value has completed streaming the value
async def handle_ready(value):
    print(f"\n[SYSTEM] Moving to Question Number: {value}")

@engine.on_complete("type") 
async def handle_progress(value: str):
    print(f"[SYSTEM] Question Category: {value}")

@engine.on_stream("text") # Prints/Streams each character as they arrive
async def handle_description_stream(delta: str, current_raw: str):
  
    print(delta, end="", flush=True)

@engine.on_complete("end")
async def handle_user(value: bool):
    print(f"\n[EVENT] Interview complete status: {value}")

# Feed your LLM stream into the engine
async def stream_from_ollama():
    print("Starting AI stream...\n")
    
    messages = [
        {
            "role": "system",
            "content": "You are a job interviewer. Reply in strict JSON with keys: question_number, type, text, and end."
        }
    ]
    
    client = AsyncClient()
    stream = await client.chat(
        model="any model",
        messages=messages,
        stream=True,
    )
    
    async for chunk in stream:
        text_chunk = chunk["message"]["content"]
        await engine.consume(text_chunk) 
        
    print("\n\nStream finished. Final Validated State:", engine.state)

if __name__ == "__main__":
    asyncio.run(stream_from_ollama())

The Roadmap

I am actively developing agentic_json and continuously releasing better versions. This is still in the prototype phase but my goal is to make this library the absolute industry standard for handling streaming JSON in agentic workflows.

Feel free to open issues whenever anything breaks, submit pull requests, and help the world build some incredibly responsive AI apps faster.

License

Apache-2.0 license

Metadata

Release files for agentic-json 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentic-json 0.1.1
File Size Uploaded
agentic_json-0.1.1.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agentic-json 0.1.1
File Interpreter ABI Platform
agentic_json-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / agentic_json-0.1.1.tar.gz

Download URL agentic_json-0.1.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b2762c0f915fee73c320c4ac624c03429e7062d036b184be7930c571d723ce93
BLAKE2b-256 checksum
How to use checksums
8648bcc17ae33355d188e371b39607a9b8834f0f22ea9f716d6806fab039de60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release files / agentic_json-0.1.1-py3-none-any.whl

Download URL agentic_json-0.1.1-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9219fec1b64457bc3d253412033f3b5ac364644659541c70086d729d7db6461
BLAKE2b-256 checksum
How to use checksums
1cd15f65a81e14ab7f1cfd87b7fc477bea2f026d7211e04f9926f1fa6bd01ac3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page