Output guard pipeline#
One constrained decoding step#
Run the output guard project#
| Command | What it does |
|---|
python3 scripts/project_test.py json-schema-output-guard --init <ws> | Creates your workspace once. A fresh starter fails |
|---|
python3 scripts/project_test.py json-schema-output-guard --stage 1 --path <ws> --strict | Grades one stage, 1 to 4 |
|---|
python3 scripts/project_test.py json-schema-output-guard --all --solution --strict | Grades the reference solution |
|---|
node cli.ts --schema samples/schema.json --attempts samples/attempts.json --output guard.json --html guard.html | Recorded repair. Attempt 1 fails at $/confidence |
|---|
Tasks in code#
| Task | Code |
|---|
| Local JSON with an FSM | generator = outlines.generate.json(model, Review) |
|---|
| Pydantic plus retries, any provider | client = instructor.from_anthropic(Anthropic()) |
|---|
| Vendor schema mode | client.beta.chat.completions.parse(..., response_format=Product) |
|---|
| Validate a dict | validate_schema(data, schema) returns a list of errors |
|---|
| Parse, validate, retry | extract_with_retry(text, schema, max_retries=3) |
|---|
| Three-branch handler | process_model_output(raw, INVOICE_SCHEMA) |
|---|
| Guard one raw string | guard(raw, schema) returns ok, value, issues |
|---|
| Bounded repair | await repair(generate, schema, maxAttempts) |
|---|
Questions#
- Should a refusal trigger a retry?
- No. Log it and return a typed refusal.
- Is constrained decoding slower?
- Often faster. Forced tokens skip generation.
- Does it need a big model?
- No. A 3B model with grammar beats a 70B model with prompts.
- Does acceptance prove the answer?
- No. It does not establish factual correctness.
- Why call checkSchema first?
- A broken schema then cannot spend provider budget.
Pydantic schema with Instructor#
class Invoice(BaseModel):
vendor: str
total_usd: float = Field(ge=0)numeric bound
line_items: list[str]
client = instructor.from_anthropic(Anthropic())
invoice = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
response_model=Invoice,retries on validation failure
messages=[{"role": "user", "content": "<text>"}],
)