Class LlmResponseUtils

java.lang.Object
dev.dokimos.core.LlmResponseUtils

public final class LlmResponseUtils extends Object
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 Type
    Method
    Description
    static String
    extractJson(String response)
    Extracts the JSON payload from an LLM response.
    static com.fasterxml.jackson.databind.ObjectMapper
    An ObjectMapper configured 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> T
    parse(String response, com.fasterxml.jackson.core.type.TypeReference<T> type)
    Extracts 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.JsonNode
    parseTree(String response)
    Extracts the JSON payload from an LLM response (dropping any surrounding prose or code fence) and parses it leniently into a tree.
    static String
    Strips markdown code block formatting from an LLM response.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • lenientMapper

      public static com.fasterxml.jackson.databind.ObjectMapper lenientMapper()
      An ObjectMapper configured 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

      public static String stripMarkdown(String response)
      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

      public static String extractJson(String response)
      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 fence
      type - 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