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:Guardrails ai Guardrails Structured Data Utils

From Leeroopedia
Revision as of 12:51, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Guardrails_ai_Guardrails_Structured_Data_Utils.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Structured_Output, LLM_Integration
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete utility functions for constructing structured output parameters provided by the guardrails package.

Description

This module provides two key utilities: json_function_calling_tool constructs an OpenAI-compatible tools list with a gd_response_tool function that encodes the Guard's output schema, and output_format_json_schema constructs a response_format dict for JSON mode with strict schema enforcement. Additionally, Guard.response_format_json_schema and Guard.json_function_calling_tool are convenience methods on the Guard class that delegate to these utilities.

Usage

Use guard.json_function_calling_tool() to generate tools for function calling strategy, or guard.response_format_json_schema() for JSON mode strategy. Pass the result as tools= or response_format= kwargs to the Guard call.

Code Reference

Source Location

  • Repository: guardrails
  • File: guardrails/utils/structured_data_utils.py (L53-74), guardrails/guard.py (L1195-1212)

Signature

# Utility function
def json_function_calling_tool(
    schema: Dict,
    tools: Optional[List] = None,
) -> List:
    """Append a gd_response_tool to the tools list."""

# Utility function
def output_format_json_schema(schema: ModelOrListOfModels) -> dict:
    """Create a response_format dict with strict JSON schema."""

# Guard method
def json_function_calling_tool(
    self,
    tools: Optional[list] = None,
) -> List[Dict[str, Any]]:
    """Appends an OpenAI tool that specifies the output structure
    using JSON Schema for chat models."""

# Guard method (experimental)
@experimental
def response_format_json_schema(self) -> Dict[str, Any]:
    """Return a response_format dict for JSON mode."""

Import

from guardrails.utils.structured_data_utils import json_function_calling_tool
from guardrails.utils.structured_data_utils import output_format_json_schema
# Or via Guard methods:
# guard.json_function_calling_tool()
# guard.response_format_json_schema()

I/O Contract

Inputs

Name Type Required Description
schema Dict Yes JSON Schema dict (for json_function_calling_tool)
schema ModelOrListOfModels Yes Pydantic model class (for output_format_json_schema)
tools Optional[List] No Existing tools list to append to

Outputs

Name Type Description
tools List[Dict] OpenAI-compatible tools list with gd_response_tool appended
response_format Dict response_format dict with json_schema key for JSON mode

Usage Examples

Function Calling Strategy

from guardrails import Guard
from pydantic import BaseModel, Field

class Person(BaseModel):
    name: str = Field(description="Full name")
    age: int = Field(description="Age in years")

guard = Guard.for_pydantic(output_class=Person)

# Generate tools parameter for function calling
tools = guard.json_function_calling_tool()

result = guard(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Generate a person."}],
    tools=tools,
)

JSON Mode Strategy

# Generate response_format for JSON mode
response_format = guard.response_format_json_schema()

result = guard(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Generate a person."}],
    response_format=response_format,
)

Related Pages

Implements Principle

Page Connections

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