Package dev.dokimos.core
Class LlmResponseUtils
java.lang.Object
dev.dokimos.core.LlmResponseUtils
Utility methods for processing LLM responses.
Provides common parsing functionality for handling responses from
JudgeLM implementations. Judges are language models, so their replies are not always
strict JSON: they may wrap the payload in a markdown fence, add a sentence of preamble, or emit
single quotes, unquoted keys, or a trailing comma. The helpers here recover the JSON payload and
parse it leniently so an otherwise correct judgment is not lost to a formatting quirk.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringextractJson(String response) Extracts the JSON payload from an LLM response.static com.fasterxml.jackson.databind.ObjectMapperAnObjectMapperconfigured to tolerate the common ways a language model deviates from strict JSON (trailing commas, single quotes, unquoted field names, comments, and unescaped control characters).static <T> TExtracts the JSON payload from an LLM response (dropping any surrounding prose or code fence) and parses it leniently into the requested type.static com.fasterxml.jackson.databind.JsonNodeExtracts the JSON payload from an LLM response (dropping any surrounding prose or code fence) and parses it leniently into a tree.static StringstripMarkdown(String response) Strips markdown code block formatting from an LLM response.
-
Method Details
-
lenientMapper
public static com.fasterxml.jackson.databind.ObjectMapper lenientMapper()AnObjectMapperconfigured to tolerate the common ways a language model deviates from strict JSON (trailing commas, single quotes, unquoted field names, comments, and unescaped control characters). Shared and thread-safe for reads.- Returns:
- the shared lenient object mapper
-
stripMarkdown
Strips markdown code block formatting from an LLM response.Many LLMs wrap JSON responses in markdown code blocks like:
```json {"key": "value"} ```This method removes such formatting to extract the raw content.- Parameters:
response- the LLM response that may contain markdown formatting- Returns:
- the response with markdown code block formatting removed
-
extractJson
Extracts the JSON payload from an LLM response. Markdown fences are removed, then the substring from the first opening brace or bracket to its matching closing character is returned, dropping any preamble or trailing prose around the JSON. When the response contains both an object and an array, the one that appears first is used. When no JSON structure is found the stripped response is returned unchanged so the caller surfaces a meaningful parse error.- Parameters:
response- the LLM response that may wrap JSON in prose or a code fence- Returns:
- the extracted JSON text, or null when the response is null
-
parse
public static <T> T parse(String response, com.fasterxml.jackson.core.type.TypeReference<T> type) throws com.fasterxml.jackson.core.JsonProcessingException Extracts the JSON payload from an LLM response (dropping any surrounding prose or code fence) and parses it leniently into the requested type.- Type Parameters:
T- the deserialized type- Parameters:
response- the LLM response that may wrap JSON in prose or a code fencetype- the target type to deserialize into- Returns:
- the parsed value
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the extracted payload cannot be parsed
-
parseTree
public static com.fasterxml.jackson.databind.JsonNode parseTree(String response) throws com.fasterxml.jackson.core.JsonProcessingException Extracts the JSON payload from an LLM response (dropping any surrounding prose or code fence) and parses it leniently into a tree.- Parameters:
response- the LLM response that may wrap JSON in prose or a code fence- Returns:
- the parsed JSON tree
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the extracted payload cannot be parsed
-