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.

Principle:Sgl project Sglang Program State Result Extraction

From Leeroopedia


Knowledge Sources
Domains Frontend_DSL, Data_Extraction, LLM_Programming
Last Updated 2026-02-10 00:00 GMT

Overview

A result extraction pattern that retrieves generated text, full conversation transcripts, and structured message lists from executed SGLang programs.

Description

After executing an SGLang generation program, the ProgramState object holds all results: generated text variables (named via sgl.gen("name")), the complete conversation text, and structured message arrays. The state supports dictionary-style access (state["name"]) for individual variables, .text() for the full prompt+generation transcript, and .messages() for a structured list of role-tagged messages suitable for logging or further processing.

Usage

Extract results from ProgramState after every .run() or .run_batch() call. Use state["name"] for specific variables, state.text() for the full conversation, and state.messages() for structured output.

Theoretical Basis

ProgramState acts as a named variable store with three views:

  1. Variable view (state["name"]) — Access individual named generations
  2. Text view (state.text()) — Full conversation as a single string
  3. Message view (state.messages()) — Structured array of {role, content} dicts

This multi-view design supports different downstream needs:

  • Variables for structured extraction (JSON fields, specific answers)
  • Text for logging and debugging
  • Messages for continued conversations or API forwarding

Related Pages

Implemented By

Page Connections

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