Packages
Extract structured data from text using LLMs with source grounding. Maps every extraction back to exact byte positions in the source. Supports Claude, OpenAI, and Gemini providers. Elixir port of google/langextract.
Current section
Files
Jump to
Current section
Files
lib/lang_extract/pipeline.ex
defmodule LangExtract.Pipeline do
@moduledoc """
Extraction pipeline: normalize LLM output, parse extractions, align to source text.
"""
alias LangExtract.Alignment.Aligner
alias LangExtract.Pipeline.Parser
alias LangExtract.Span
alias LangExtract.WireFormat
@spec extract(String.t(), String.t(), keyword()) ::
{:ok, [Span.t()]}
| {:error, {:invalid_format, String.t()} | :missing_extractions}
def extract(source, raw_llm_output, opts) do
with {:ok, normalized} <- WireFormat.normalize(raw_llm_output),
{:ok, extractions} <- Parser.parse(normalized) do
texts = Enum.map(extractions, & &1.text)
spans = Aligner.align(source, texts, opts)
enriched =
Enum.zip_with(extractions, spans, fn extraction, %Span{} = span ->
%Span{span | class: extraction.class, attributes: extraction.attributes}
end)
{:ok, enriched}
end
end
end