Sgr

From fortunto2/solo-factory · View interactive version

Sgr is an AI agent skill from the GitHub repository fortunto2/solo-factory. Use when "design schemas", "structured output", "agent loop", "SGR", "constrained decoding", "tool dispatch", "Pydantic schema for LLM", or need to design a schema-guided reasoning pipeline for an agent or API. Do NOT use for general code review (/review) or planning (/plan).

How to use this skill

Skill file

/sgr

Design and implement Schema-Guided Reasoning (SGR) pipelines. Translate domain expert mental checklists into structured reasoning schemas for LLMs.

Source: Rinat Abdullin — Schema-Guided Reasoning

Core Principle

SGR = guide LLM reasoning through predefined steps via constrained decoding. Instead of free-form text → enforce a schema that defines what steps, in which order, where to focus attention.

Domain expert mental checklist → Pydantic/Zod schema → Constrained decoding → Deterministic dispatch

When to Use

Steps

  1. Parse task from $ARGUMENTS:
  1. Identify the reasoning cascade — interview the domain:

This is the critical step. SGR quality = how well you translate the expert's mental checklist.

  1. Design the schema following SGR patterns:

### The NextStep Pattern (agent loop)

```python

class NextStep(BaseModel):

current_state: str # thinking space

plan_remaining_steps: list[str] # 1-5 steps, only first used

task_completed: bool # routing gate

function: Union[Tool1, Tool2, ..., ReportCompletion] = Field(

..., description="execute first remaining step"

)

```

### The Analysis Cascade Pattern (single-shot)

```python

class Analysis(BaseModel):

preliminary: str # initial assessment

classification: Literal["a", "b", "c"] # force categorization

evidence: list[str] # cite sources

gaps: list[GapItem] # structured findings

verdict: Literal["pass", "partial", "fail"] # final decision

reasoning_for_verdict: str # explain after deciding

```

### The Tool Dispatch Pattern

```python

class SendEmail(BaseModel):

tool: Literal["send_email"] # discriminator

recipient: str

subject: str

body: str

class SearchDB(BaseModel):

tool: Literal["search_db"]

query: str

# Union with Literal discriminator = deterministic routing

Action = Union[SendEmail, SearchDB, ReportDone]

```

  1. Apply SGR design rules (from references/sgr-rules.md):
  1. Implement the dispatch loop (if agent):

```python

for i in range(MAX_STEPS):

response = client.beta.chat.completions.parse(

model=MODEL,

response_format=NextStep,

messages=log,

)

job = response.choices[0].message.parsed

if isinstance(job.function, ReportCompletion):

break # done

result = dispatch(job.function) # deterministic routing

log.append(assistant_message(job))

log.append(tool_result(result))

```

  1. Add to project:
  1. Audit mode (if $ARGUMENTS = "audit"):

Output

## SGR Pipeline: {domain}

**Pattern:** {NextStep | Analysis Cascade | Tool Dispatch}
**Schemas:** {N} models
**Tools:** {N} (if agent loop)

### Reasoning Cascade
{step 1} → {step 2} → ... → {decision/action}

### Files
- schemas/{name}.py — {N} models
- dispatch.py — tool routing
- tests/test_{name}.py — validation tests

Key References

Libraries & Implementations

Rust

In Rust, SGR is even stronger: #[serde(tag = "tool")] gives discriminated union dispatch at zero runtime cost. Enum variants = tools, serde deserialization = constrained decoding.

Python

Common Issues

Schema too flat

Cause: Tried to put everything in one model.

Fix: Split into analysis model + action model. Cascade, don't flatten.

LLM ignores enum constraints

Cause: Model not supporting constrained decoding, or wrong API.

Fix: Use response_format=Schema (OpenAI), tools with schema (Anthropic). Check references/sgr-rules.md for provider-specific notes.

Agent loops forever

Cause: No task_completed gate or ReportCompletion tool.

Fix: Always include a completion signal in the Union. Cap loop iterations.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.