Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Predibase Lorax JSON Response Parsing

From Leeroopedia


Knowledge Sources
Domains Structured_Output, Data_Processing
Last Updated 2026-02-08 02:00 GMT

Overview

Concrete tool for parsing schema-constrained model output into structured Python objects, using the standard json module and optional Pydantic validation.

Description

This is an External Tool Doc for the standard Python json.loads() function used in the context of LoRAX structured output. When response_format with a JSON schema was used in the request, the Response.generated_text is guaranteed to be valid JSON conforming to that schema. The parsing step simply converts the JSON string to a Python dict.

For additional type safety, the parsed dict can be validated against a Pydantic model using model_validate().

Usage

Called by the user after receiving a response from a schema-constrained request. This is a client-side operation.

Code Reference

Source Location

  • Repository: Python standard library
  • File: json (stdlib)

Signature

import json

# Standard JSON parsing
data: dict = json.loads(response.generated_text)

# Optional Pydantic validation
from pydantic import BaseModel
class MySchema(BaseModel):
    name: str
    score: float

result: MySchema = MySchema.model_validate(data)

Import

import json

I/O Contract

Inputs

Name Type Required Description
Response.generated_text str Yes JSON string from schema-constrained generation

Outputs

Name Type Description
parsed_data dict Python dictionary conforming to the request schema
typed_result BaseModel Pydantic model instance (if validated)

Usage Examples

Complete Structured Output Pipeline

import json
from pydantic import BaseModel
from lorax import Client
from lorax.types import ResponseFormat

# Define schema
class MovieReview(BaseModel):
    title: str
    rating: float
    summary: str
    recommend: bool

# Generate with schema constraint
client = Client("http://localhost:3000")
response = client.generate(
    "Review the movie 'Inception' in JSON format.",
    response_format=ResponseFormat(
        type="json_object",
        schema=MovieReview.model_json_schema(),
    ),
    adapter_id="review-adapter",
    max_new_tokens=200,
)

# Parse (guaranteed valid JSON)
data = json.loads(response.generated_text)
review = MovieReview.model_validate(data)
print(f"{review.title}: {review.rating}/10")
print(f"Recommend: {review.recommend}")

OpenAI-Compatible Structured Output

from openai import OpenAI
import json

client = OpenAI(base_url="http://localhost:3000/v1", api_key="x")

response = client.chat.completions.create(
    model="my-adapter",
    messages=[
        {"role": "user", "content": "List 3 programming languages as JSON"}
    ],
    response_format={
        "type": "json_object",
        "schema": {
            "type": "object",
            "properties": {
                "languages": {
                    "type": "array",
                    "items": {"type": "string"}
                }
            },
            "required": ["languages"]
        }
    },
    max_tokens=100,
)

data = json.loads(response.choices[0].message.content)
print(data["languages"])  # ["Python", "Rust", "JavaScript"]

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment