Skip to main content

Get Running in 5 Minutes

This guide will get you from zero to working GLYPH code in under 5 minutes.

Installation

Choose your language:
pip install glyph-serial
See the Installation Guide for detailed setup instructions and requirements.

Basic Usage

1

Import GLYPH

import glyph
2

Convert JSON to GLYPH

# Your existing Python data
data = {
    "action": "search",
    "query": "AI agents",
    "limit": 5
}

# Convert to GLYPH (one line)
glyph_str = glyph.json_to_glyph(data)
print(glyph_str)
# Output: {action=search limit=5 query="AI agents"}
3

Convert GLYPH back to JSON

# Convert back to Python dict
restored = glyph.glyph_to_json(glyph_str)
assert restored == data  # Perfect round-trip
4

Parse GLYPH directly

# Parse GLYPH text
result = glyph.parse('{name=Alice age=30 active=t}')

# Access fields
print(result.get("name").as_str())    # "Alice"
print(result.get("age").as_int())     # 30
print(result.get("active").as_bool()) # True

Token Savings Example

Let’s see the token savings in action:
import glyph
import json

# Sample data: API response with search results
data = {
    "status": "success",
    "results": [
        {"id": "doc_1", "title": "GLYPH Guide", "score": 0.95},
        {"id": "doc_2", "title": "API Docs", "score": 0.89},
        {"id": "doc_3", "title": "Tutorial", "score": 0.84}
    ],
    "total": 3
}

# Convert to both formats
json_str = json.dumps(data)
glyph_str = glyph.json_to_glyph(data)

# Compare
print(f"JSON:  {len(json_str)} chars")
print(f"GLYPH: {len(glyph_str)} chars")
print(f"Savings: {100 * (1 - len(glyph_str)/len(json_str)):.0f}%")

# Output:
# JSON:  156 chars
# GLYPH: 98 chars
# Savings: 37%
These are character counts. Token savings (what LLMs count) are typically even higher—40-60% for most data shapes.

Streaming Validation

One of GLYPH’s killer features: validate tool calls as tokens stream, not after generation completes.
Why this matters: Catch bad tool names, missing params, or constraint violations at token 3-5, not token 50+. Save tokens, time, and reduce failures.

Example: Validate Tool Calls

Python
import glyph
from glyph import StreamingValidator, ToolRegistry

# Define your tools
registry = ToolRegistry()

registry.register(
    name="search",
    args={
        "query": {"type": "str", "required": True, "min_len": 1},
        "max_results": {"type": "int", "min": 1, "max": 100, "default": 10}
    }
)

registry.register(
    name="calculate",
    args={
        "expression": {"type": "str", "required": True}
    }
)

# Create validator
validator = StreamingValidator(registry)

# Simulate streaming tokens from LLM
tokens = ["{", "tool", "=", "unknown", "query", "=", "test", "}"]

for token in tokens:
    result = validator.push(token)
    
    # Tool name detected
    if result.tool_name:
        print(f"Tool detected: {result.tool_name}")
        
        # Unknown tool? Cancel immediately!
        if not result.tool_allowed:
            print(f"ERROR: Unknown tool '{result.tool_name}'")
            print("Cancelling generation...")
            break
    
    # Check for errors
    if result.has_errors():
        print(f"Validation error: {result.errors}")
        break

# Output:
# Tool detected: unknown
# ERROR: Unknown tool 'unknown'
# Cancelling generation...
Result: Error detected at token 4, not after full generation (50+ tokens).

Auto-Tabular Mode

When you have lists of similar objects, GLYPH automatically uses tabular encoding:
import glyph

# List of similar objects (3+ items)
search_results = [
    {"id": "doc_1", "score": 0.95, "title": "Introduction"},
    {"id": "doc_2", "score": 0.89, "title": "Guide"},
    {"id": "doc_3", "score": 0.84, "title": "Reference"}
]

# Convert to GLYPH (automatically uses tabular format)
text = glyph.json_to_glyph(search_results)
print(text)
Why it’s efficient: Keys appear once for the entire table, not repeated for every row.

Parse Tabular Data

# Parse tabular GLYPH
parsed = glyph.parse(text)

# Access as list
print(f"Found {len(parsed)} results")

# Access first result
first = parsed.index(0)
print(first.get("id").as_str())    # "doc_1"
print(first.get("score").as_float()) # 0.95

# Convert back to Python list of dicts
results = glyph.to_json(parsed)
# results is identical to original search_results

JSON Interoperability

GLYPH is a drop-in replacement for JSON with perfect round-trip conversion:
1

Start with any Python/JS data

Works with dicts, lists, primitives, nested structures—anything JSON supports.
2

Convert to GLYPH

glyph_text = glyph.json_to_glyph(data)
40-60% smaller, still human-readable.
3

Store or transmit

Save to database, send over network, include in LLM prompt—50%+ token savings.
4

Convert back when needed

restored = glyph.glyph_to_json(glyph_text)
Perfect reconstruction of original data.

Gradual Migration

You don’t need to rewrite everything. Migrate gradually:
import json
import glyph

# Phase 1: Generate JSON (what LLMs know)
llm_output = '{"action": "search", "query": "test"}'
parsed = json.loads(llm_output)

# Phase 2: Store as GLYPH (40% smaller)
stored = glyph.json_to_glyph(parsed)
save_to_db(stored)

# Phase 3: Load and convert back for next LLM
loaded = load_from_db()
as_json = glyph.glyph_to_json(loaded)
llm_context = json.dumps(as_json)

Format Quick Reference

Null:    _              List:    [1 2 3]
Bool:    t / f          Map:     {a=1 b=2}
Int:     42             String:  hello or "hello world"
Float:   3.14           Struct:  User{name=Alice age=30}
vs JSON:
  • No commas between elements
  • = instead of :
  • Bare strings (no quotes) when unambiguous
  • t/f instead of true/false
  • _ or instead of null

More Examples

{
  "user": "alice",
  "active": true,
  "score": 0.95,
  "tags": ["premium", "beta"],
  "metadata": null
}

Next Steps

You now have the basics! Here’s what to explore next:

Installation Guide

Detailed setup for all languages

API Reference

Complete API documentation

Agent Patterns

Tool calling, state management, ReAct loops

Cookbook

10 practical recipes

Common Questions

Yes, but they’re more reliable with JSON (what they’re trained on). Best practice: LLMs generate JSON, you serialize to GLYPH for storage/transmission.
Parsing speed is comparable to JSON. The real benefit is token efficiency—40-60% fewer tokens means lower costs and more context space.
Those are binary formats—not human-readable and LLMs can’t parse them. GLYPH is text-based, debuggable, and LLM-friendly.
Yes! Convert at the boundaries:
# Receive JSON from API
api_response = requests.get(...).json()

# Store as GLYPH
stored = glyph.json_to_glyph(api_response)

# Convert back when calling another API
next_api_data = glyph.glyph_to_json(stored)

Get Help

Build docs developers (and LLMs) love