Verified Owner
Top Description Fields Constructors Methods
org.jabref.logic.ai.util

public final Class LlmResponseCleaner

extends Object
Class Inheritance
Imports
org.jspecify.annotations.Nullable

Cleans raw LLM response strings by extracting content from the last fenced code block (``` ... ```) if present, or simply trimming whitespace.

Rules:

  1. If the response contains no ``` fences → return the string stripped of leading/trailing whitespace.
  2. If one or more ``` fences exist → find the last complete block, strip the optional language label on the opening fence (e.g. ```json or ```markdown), and return the inner content trimmed.
  3. If the last fence is unclosed (no matching closing ```) → treat everything after the opening fence line as the content.

Field Summary

Modifier and TypeField and Description
private static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the cleaned string, never null
clean
(@Nullable String
the raw LLM response; may be null
response
)

Cleans the given LLM response string according to the rules above.

private static int
findLastOpener(String s)

Returns the start index of the last "opening" ``` in the string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FENCEback to summary
private static final String FENCE

Constructor Detail

LlmResponseCleanerback to summary
private LlmResponseCleaner()

Method Detail

cleanback to summary
public static String clean(@Nullable String response)

Cleans the given LLM response string according to the rules above.

Parameters
response:@Nullable String

the raw LLM response; may be null

Returns:String

the cleaned string, never null

findLastOpenerback to summary
private static int findLastOpener(String s)

Returns the start index of the last "opening" ``` in the string.

We treat every as a potential opener/closer pair. Walking through the string we toggle a flag: the first opens a block, the next closes it, etc. We remember the start index of each opener and return the last one seen.