Stop waiting for your LLM to finish generating. agentic_json is a real-time streaming parser with a clean decorator syntax, letting your AI agents process and react to structured data mid-stream.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 agentic_json-0.1.1.tar.gz.
File metadata
- Download URL: agentic_json-0.1.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2762c0f915fee73c320c4ac624c03429e7062d036b184be7930c571d723ce93
|
|
| MD5 |
0091d8fb413eba8f74c2bd747678fb38
|
|
| BLAKE2b-256 |
8648bcc17ae33355d188e371b39607a9b8834f0f22ea9f716d6806fab039de60
|
File details
Details for the file agentic_json-0.1.1-py3-none-any.whl.
File metadata
- Download URL: agentic_json-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9219fec1b64457bc3d253412033f3b5ac364644659541c70086d729d7db6461
|
|
| MD5 |
269892f44e60a7f5d4dc46a3e5f89348
|
|
| BLAKE2b-256 |
1cd15f65a81e14ab7f1cfd87b7fc477bea2f026d7211e04f9926f1fa6bd01ac3
|