parser

package
v0.206.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package parser forgivingly extracts a JSON object from messy model output. Grammar-constrained decoding already yields valid JSON in the common case; this is the safety net for fenced blocks, leading/trailing prose, and minor defects (trailing commas, smart quotes).

Index

Constants

This section is empty.

Variables

View Source
var ErrNoJSON = errors.New("no JSON object found in output")
View Source
var ErrNotOneObject = errors.New("reply is not exactly one JSON object")

ErrNotOneObject is ExtractOne's refusal of a reply that holds something other than exactly one JSON object: a top-level array, a second value after the object, or a repeated key.

Functions

func Extract

func Extract(raw string) (json.RawMessage, error)

Extract returns the first valid JSON object found in raw, as compact bytes.

func ExtractOne added in v0.206.0

func ExtractOne(raw string) (json.RawMessage, error)

ExtractOne is Extract for a seat whose decoding nothing constrains (config unconstrained_seats). A grammar guarantees one object; without one, Extract's "first object wins" would accept an answer the model did not commit to, so this refuses it instead and the existing correction retry asks again. After fence stripping and trimming:

  • the first JSON value must be an object: a top-level array (even of one object) is refused, and so is a `[` before the first `{`;
  • nothing after that object may start another value: any `{` or `[` in the remainder is refused (`{..} {..}`), while trailing prose without one ("hope that helps") is still accepted, as is whatever follows a closing code fence (stripFences drops it);
  • leading prose before the object is still accepted, as Extract does;
  • no object at any depth may repeat a key (encoding/json keeps the last one silently).

Grammar seats never reach this function; they keep Extract byte for byte.

func StripThink

func StripThink(s string) string

StripThink removes a leading <think>…</think> reasoning span (emitted by the reasoning tier's think-prefixed grammar, see gbnf.WrapThinking) so the remainder is the bare JSON that Extract expects — needed for bare-string outputs (classify/triage enums) where the first-object-span fallback can't find the value. The grammar guarantees the output starts with "<think>" and that the FIRST "</think>" is the structural separator (the think rule forbids "</think>" inside reasoning); a later "</think>" therefore belongs to the JSON answer and must be preserved — so split on the FIRST close tag, and only when the content actually opens with <think> (a non-reasoning response that merely contains </think> is left untouched). Returns s unchanged if it is not a think-wrapped response.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL