Skip to content

fix: guard against None input in trimAndLoadJson#2620

Open
bongho wants to merge 1 commit intoconfident-ai:mainfrom
bongho:fix/trim-load-json-none-guard
Open

fix: guard against None input in trimAndLoadJson#2620
bongho wants to merge 1 commit intoconfident-ai:mainfrom
bongho:fix/trim-load-json-none-guard

Conversation

@bongho
Copy link
Copy Markdown

@bongho bongho commented Apr 19, 2026

Problem

When the evaluation LLM returns None (instead of a string), trimAndLoadJson crashes with:

AttributeError: 'NoneType' object has no attribute 'find'

This happens because the function calls input_string.find("{") without first checking whether input_string is None.

Related issue: #2554

Solution

  • Changed the type hint of input_string from str to Optional[str]
  • Added an explicit None guard at the top of the function that:
    • Raises a descriptive ValueError with the same message used for other JSON parse errors
    • Sets metric.error when a metric is provided (consistent with existing error handling)

Changes

 def trimAndLoadJson(
-    input_string: str,
+    input_string: Optional[str],
     metric: Optional[BaseMetric] = None,
 ) -> Any:
+    if input_string is None:
+        error_str = "Evaluation LLM outputted an invalid JSON. Please use a better evaluation model."
+        if metric is not None:
+            metric.error = error_str
+        raise ValueError(error_str)
+
     start = input_string.find("{")

Testing

The fix is minimal and follows the existing error-handling pattern already present in the function's except json.JSONDecodeError block.

Fixes AttributeError when LLM returns None instead of a string.
Added an explicit None check that raises a descriptive ValueError and
sets metric.error when a metric is provided, consistent with the
existing error handling pattern in this function.

Fixes confident-ai#2554
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 19, 2026

@bongho is attempting to deploy a commit to the Confident AI Team on Vercel.

A member of the Team first needs to authorize it.

@bongho
Copy link
Copy Markdown
Author

bongho commented Apr 23, 2026

Hi team! Just a friendly ping on this PR. Wanted to note that Issue #2554 was previously addressed by PR #2558 (closed by its author due to CI environment issues, not because of the fix itself). This PR picks up where that left off with the same approach — a minimal None guard in trimAndLoadJson that follows the existing error-handling pattern. The upstream CI failures (missing CONFIDENT_API_KEY, Black formatting) are pre-existing and unrelated to this change. Happy to address any review feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant