Extracts JSON from LLM responses, handling common formatting quirks.
LLMs often wrap JSON in markdown code blocks, add explanatory text, or include trailing commentary. This module extracts the JSON content following a priority order:
- JSON in
json code block 2. JSON incode block (no language) - Raw JSON object (starts with
{) - Raw JSON array (starts with
[)
Examples
iex> PtcRunner.SubAgent.JsonParser.parse(~s|{"name": "Alice"}|)
{:ok, %{"name" => "Alice"}}
iex> PtcRunner.SubAgent.JsonParser.parse("```json\n{\"a\": 1}\n```")
{:ok, %{"a" => 1}}
iex> PtcRunner.SubAgent.JsonParser.parse("Here's the result: {\"x\": 5}")
{:ok, %{"x" => 5}}
iex> PtcRunner.SubAgent.JsonParser.parse("No JSON here")
{:error, :no_json_found}
Summary
Functions
Parse JSON from an LLM response string.
Functions
Parse JSON from an LLM response string.
Extracts JSON from code blocks or raw content, handling common LLM formatting quirks like trailing text or explanation prefixes.
Returns {:ok, term()} with the parsed JSON data, or an error tuple.
Error Types
{:error, :no_json_found}- No JSON structure detected in the response{:error, :invalid_json}- JSON was found but failed to parse
Examples
iex> PtcRunner.SubAgent.JsonParser.parse(~s|{"count": 42}|)
{:ok, %{"count" => 42}}
iex> PtcRunner.SubAgent.JsonParser.parse("[1, 2, 3]")
{:ok, [1, 2, 3]}
iex> PtcRunner.SubAgent.JsonParser.parse("```json\n{\"valid\": true}\n```")
{:ok, %{"valid" => true}}
iex> PtcRunner.SubAgent.JsonParser.parse("plain text")
{:error, :no_json_found}
iex> PtcRunner.SubAgent.JsonParser.parse("```json\n{invalid}\n```")
{:error, :invalid_json}