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:Sgl project Sglang Program State Getitem

From Leeroopedia


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

Overview

Concrete tool for extracting generated variables and conversation data from SGLang ProgramState objects.

Description

The ProgramState class provides dictionary-style access to named generation variables via __getitem__ (state["name"]). The .text() method returns the full conversation as a concatenated string. The .messages() method returns a list of role/content dicts. The .get_meta_info("name") method retrieves metadata (token counts, finish reason) for a specific variable.

Usage

Access named variables with state["name"] after program execution. Use .text() for debugging and .messages() for structured conversation export.

Code Reference

Source Location

  • Repository: sglang
  • File: python/sglang/lang/interpreter.py
  • Lines: L992-993 (__getitem__), L882-883 (text), L885-886 (messages)

Signature

class ProgramState:
    def __getitem__(self, name: str) -> str:
        """Get generated text for a named variable."""
        return self.get_var(name)

    def text(self) -> str:
        """Get the full conversation text."""
        return self.stream_executor.text()

    def messages(self) -> List[Dict]:
        """Get structured message list [{role, content}, ...]."""
        return self.stream_executor.messages()

    def get_meta_info(self, name: str) -> Dict:
        """Get metadata for a named variable."""
        return self.stream_executor.get_meta_info(name)

Import

# ProgramState is returned by SglFunction.run()
state = my_function.run(...)
result = state["variable_name"]

I/O Contract

Inputs

Name Type Required Description
name str Yes (for __getitem__) Variable name passed to sgl.gen("name")

Outputs

Name Type Description
state["name"] str Generated text for the named variable
state.text() str Full conversation text (all roles concatenated)
state.messages() List[Dict] Structured messages [{role: str, content: str}, ...]
state.get_meta_info("name") Dict Metadata (token counts, finish reason)

Usage Examples

Variable Access

import sglang as sgl

@sgl.function
def qa(s, question):
    s += sgl.user(question)
    s += sgl.assistant(sgl.gen("answer", max_tokens=128))

state = qa.run(question="What is Python?")

# Access named variable
print(state["answer"])

# Full conversation text
print(state.text())

# Structured messages
for msg in state.messages():
    print(f"{msg['role']}: {msg['content'][:50]}...")

Multi-Variable Extraction

@sgl.function
def analyze(s, text):
    s += sgl.user(f"Analyze: {text}")
    s += sgl.assistant(sgl.gen("analysis", max_tokens=200))
    s += sgl.user("Rate the sentiment 1-5:")
    s += sgl.assistant(sgl.gen("rating", regex=r"[1-5]", max_tokens=1))

state = analyze.run(text="I love this product!")
print(f"Analysis: {state['analysis']}")
print(f"Rating: {state['rating']}")

Metadata Access

state = qa.run(question="Explain quantum physics.")
meta = state.get_meta_info("answer")
print(f"Tokens generated: {meta.get('completion_tokens', 'N/A')}")
print(f"Finish reason: {meta.get('finish_reason', 'N/A')}")

Related Pages

Implements Principle

Page Connections

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