Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Sgl project Sglang Build Regex From Object

From Leeroopedia


Knowledge Sources
Domains NLP, Structured_Generation, Constrained_Decoding
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for converting JSON schemas and Pydantic models into regex patterns for constrained text generation.

Description

The build_regex_from_object function accepts a JSON schema (as string, dict, or Pydantic BaseModel class) and returns a compiled regex string. This regex can then be passed to sgl.gen(regex=...) or the regex sampling parameter to constrain generation. Internally it delegates to the outlines library's build_regex_from_schema function.

Usage

Call build_regex_from_object when you have a Pydantic model or JSON schema and need to generate text that conforms to it. For simple regex patterns (dates, numbers), pass the regex directly without this function.

Code Reference

Source Location

  • Repository: sglang
  • File: python/sglang/srt/constrained/outlines_backend.py
  • Lines: L181-190

Signature

def build_regex_from_object(
    object: Union[str, BaseModel, Dict],
    whitespace_pattern: Optional[str] = None,
) -> str:
    """
    Convert a JSON schema to a regex pattern.

    Args:
        object: JSON schema as string, Pydantic BaseModel class, or dict.
        whitespace_pattern: Optional regex for whitespace handling.

    Returns:
        Compiled regex string for constrained generation.
    """

Import

from sglang.srt.constrained.outlines_backend import build_regex_from_object

I/O Contract

Inputs

Name Type Required Description
object Union[str, BaseModel, Dict] Yes JSON schema as string, Pydantic model class, or dict
whitespace_pattern Optional[str] No Regex pattern for whitespace handling

Outputs

Name Type Description
regex str Compiled regex string matching all valid JSON conforming to the schema

Usage Examples

From Pydantic Model

from pydantic import BaseModel
from sglang.srt.constrained.outlines_backend import build_regex_from_object

class UserInfo(BaseModel):
    name: str
    age: int
    email: str

regex = build_regex_from_object(UserInfo)
# regex can be passed to Engine.generate(sampling_params={"regex": regex})

From JSON Schema String

import json

schema = json.dumps({
    "type": "object",
    "properties": {
        "city": {"type": "string"},
        "temperature": {"type": "number"},
    },
    "required": ["city", "temperature"],
})

regex = build_regex_from_object(schema)

Related Pages

Implements Principle

Requires Environment

Page Connections

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