Principle:Sgl project Sglang Program State Result Extraction
| 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:
- Variable view (state["name"]) — Access individual named generations
- Text view (state.text()) — Full conversation as a single string
- 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