> ## Documentation Index
> Fetch the complete documentation index at: https://cognis.vasanth.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Parsers

> Output-fixing and retry parsers for when an LLM emits dirty JSON.

Parser examples enact what happens when the LLM gets the JSON contract slightly wrong. Sources under [`examples/parsers/`](https://github.com/0xvasanth/cognis/tree/main/examples/parsers).

| Name             | Scenario                                                                                                                                                     | Source                                                                                 |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- |
| `parsers_fixing` | LLM emits malformed JSON — `OutputFixingParser` re-prompts with the parse error, the model usually fixes it on the second try.                               | [src](https://github.com/0xvasanth/cognis/blob/main/examples/parsers/fixing_parser.rs) |
| `parsers_retry`  | When one fix isn't enough — `RetryParser` loops the fixer + parse cycle up to N times until parsing succeeds, surfacing the last error if all attempts fail. | [src](https://github.com/0xvasanth/cognis/blob/main/examples/parsers/retry_parser.rs)  |

## How to run

```bash theme={null}
cargo run -p cognis-examples --example parsers_fixing
cargo run -p cognis-examples --example parsers_retry
```

## When to use which

* **`OutputFixingParser`** — model is *capable* of valid JSON but slipped this time. Cheaper: one repair attempt with the parse error in the prompt.
* **`RetryParser`** — one repair pass isn't enough; loop fixer + parse up to N times. More expensive but more robust against models that produce different-but-still-broken JSON each attempt.

In production, layer them: `RetryParser::with_retries(OutputFixingParser::new(inner, fixer), 3)`.

## See also

<CardGroup cols={2}>
  <Card title="Structured output" icon="brackets-curly" href="/building-agents/structured-output">
    The user guide for parsers and recovery strategies.
  </Card>

  <Card title="Examples → Chains" icon="link" href="/examples/chains">
    `chains_structured_parsing` shows the parsers in a chain.
  </Card>
</CardGroup>
