parser

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package parser extracts structured content from LLM responses.

Core types:

  • Response: Contains structured data extracted from an LLM response
  • CodeBlock: A fenced code block with language and content
  • Parser: Extracts code blocks, JSON, YAML, sections, and lists

Example usage:

p := parser.NewParser()
resp := p.Parse(llmOutput)

// Access extracted code blocks
for _, block := range resp.CodeBlocks {
    fmt.Printf("Language: %s\nCode:\n%s\n", block.Language, block.Content)
}

// Access parsed JSON
for _, data := range resp.JSONBlocks {
    fmt.Printf("JSON: %v\n", data)
}

Convenience functions:

json := parser.ExtractJSON(response)
code := parser.ExtractCode(response, "go")
parsed := parser.Parse(response)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractCode

func ExtractCode(response, language string) string

ExtractCode is a convenience function for code extraction.

func ExtractJSON

func ExtractJSON(response string) map[string]any

ExtractJSON is a convenience function for JSON extraction.

Types

type CodeBlock

type CodeBlock struct {
	// Language is the language specifier after the opening fence (e.g., "go", "python").
	Language string

	// Content is the code inside the block, excluding fences.
	Content string

	// Raw is the complete block including the fences.
	Raw string
}

CodeBlock represents a fenced code block.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser extracts structured content from LLM responses.

func NewParser

func NewParser() *Parser

NewParser creates a new response parser with compiled regexes.

func (*Parser) ExtractAllCode

func (p *Parser) ExtractAllCode(response string) []CodeBlock

ExtractAllCode extracts all code blocks from the response.

func (*Parser) ExtractCode

func (p *Parser) ExtractCode(response, language string) string

ExtractCode extracts the first code block with the given language. If language is empty, returns the first code block found.

func (*Parser) ExtractJSON

func (p *Parser) ExtractJSON(response string) map[string]any

ExtractJSON extracts and parses the first JSON block found. Returns nil if no valid JSON block is found.

func (*Parser) ExtractJSONArray

func (p *Parser) ExtractJSONArray(response string) []map[string]any

ExtractJSONArray extracts and parses JSON arrays from code blocks. Returns all successfully parsed arrays.

func (*Parser) ExtractList

func (p *Parser) ExtractList(response string) []string

ExtractList extracts list items from the response. Supports both - and * bullet points.

func (*Parser) ExtractNumberedList

func (p *Parser) ExtractNumberedList(response string) []string

ExtractNumberedList extracts numbered list items.

func (*Parser) ExtractSection

func (p *Parser) ExtractSection(response, title string) string

ExtractSection extracts the content of a specific section by title.

func (*Parser) ExtractYAML

func (p *Parser) ExtractYAML(response string) []map[string]any

ExtractYAML extracts and parses YAML blocks.

func (*Parser) HasCodeBlock

func (p *Parser) HasCodeBlock(response string) bool

HasCodeBlock checks if the response contains any code block.

func (*Parser) HasJSON

func (p *Parser) HasJSON(response string) bool

HasJSON checks if the response contains valid JSON.

func (*Parser) Parse

func (p *Parser) Parse(response string) *Response

Parse extracts structured content from an LLM response.

type Response

type Response struct {
	// Raw is the original response text.
	Raw string

	// Text is the response with code blocks removed.
	Text string

	// CodeBlocks contains all extracted code blocks.
	CodeBlocks []CodeBlock

	// JSONBlocks contains parsed JSON blocks.
	JSONBlocks []map[string]any

	// Sections contains extracted markdown sections by title.
	Sections map[string]string
}

Response contains structured data extracted from an LLM response.

func Parse

func Parse(response string) *Response

Parse is a convenience function using the default parser.

Jump to

Keyboard shortcuts

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