Implementation:Sgl project Sglang Json Output Parsing
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Validation, Structured_Generation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Wrapper documentation for parsing and validating JSON output from constrained generation using standard library json and Pydantic.
Description
This covers two external APIs used together: json.loads() from the Python standard library for basic JSON parsing, and BaseModel.model_validate_json() from Pydantic for type-safe validation. When used after SGLang's constrained generation, syntactic validity is already guaranteed, so these calls primarily provide type conversion and semantic validation.
Usage
Call json.loads() on the generated text to get a Python dict. For type-safe objects, call YourModel.model_validate_json() to get a validated Pydantic model instance.
Code Reference
Source Location
- Library: Python stdlib json module
- Library: Pydantic (external)
- Usage pattern: examples/frontend_language/usage/json_decode.py
Signature
# Standard library
import json
result_dict = json.loads(generated_text: str) -> dict
# Pydantic validation
from pydantic import BaseModel
result_model = YourModel.model_validate_json(generated_text: str) -> YourModel
Import
import json
from pydantic import BaseModel
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| generated_text | str | Yes | JSON text from constrained generation |
Outputs
| Name | Type | Description |
|---|---|---|
| result_dict | dict | Parsed Python dictionary (via json.loads) |
| result_model | BaseModel | Validated Pydantic model instance (via model_validate_json) |
Usage Examples
Basic JSON Parsing
import json
output = engine.generate(
"Generate person info as JSON:",
{"regex": regex_from_schema, "max_new_tokens": 128},
)
# Parse to dict
data = json.loads(output["text"])
print(data["name"], data["age"])
Pydantic Validation
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
email: str
output = engine.generate(prompt, {"regex": regex, "max_new_tokens": 128})
# Validate with type checking
person = Person.model_validate_json(output["text"])
print(person.name) # Typed str
print(person.age) # Typed int