Skip to content

Accept null score in hf-inference image-segmentation response#2197

Open
Sanjays2402 wants to merge 1 commit into
huggingface:mainfrom
Sanjays2402:fix/image-segmentation-null-score
Open

Accept null score in hf-inference image-segmentation response#2197
Sanjays2402 wants to merge 1 commit into
huggingface:mainfrom
Sanjays2402:fix/image-segmentation-null-score

Conversation

@Sanjays2402
Copy link
Copy Markdown

@Sanjays2402 Sanjays2402 commented May 27, 2026

Closes #1430.

What

Loosens the HFInferenceImageSegmentationTask.getResponse type guard to accept score === null in addition to undefined and number.

Why

The image-segmentation output schema in packages/tasks/src/tasks/image-segmentation/inference.ts declares score as optional:

score?: number;

…but the runtime validator in packages/inference/src/providers/hf-inference.ts only accepted undefined or a number, rejecting null with:

API Implementation Error: TypeError: Invalid output: output must be of type
Array<{label:string; score:number; mask: string}>

Several HF-Inference models (e.g. jonathandinu/face-parsing, the one in the original report) return null for score on per-segment masks. That's consistent with the optional schema field, but the strict type guard treats it as a malformed response.

Diff

 (x) =>
     typeof x.label === "string" &&
     typeof x.mask === "string" &&
-    (x.score === undefined || typeof x.score === "number"),
+    (x.score === undefined || x.score === null || typeof x.score === "number"),

Out of scope

  • Tightening inference.ts to make score explicitly nullable — leaving the schema as-is to avoid breaking anyone reading it.
  • Other tasks: this is the only validator in hf-inference.ts whose schema declares score as optional, so the same null-acceptance change isn't symmetric across the file.

Note

Low Risk
Single-task response normalization in the HF-Inference provider with no auth, persistence, or API surface changes.

Overview
Fixes HF-Inference image-segmentation responses that were rejected when models return score: null (e.g. jonathandinu/face-parsing), which previously triggered a malformed-output error despite optional score in the task schema.

HFInferenceImageSegmentationTask.getResponse now accepts null in its guard and normalizes null scores to undefined so returned data matches score?: number and callers using score !== undefined behave correctly.

Reviewed by Cursor Bugbot for commit ad3b9ca. Bugbot is set up for automated code reviews on this repo. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2c5934d. Configure here.

Comment thread packages/inference/src/providers/hf-inference.ts
The image-segmentation output schema declares score as optional
(packages/tasks/src/tasks/image-segmentation/inference.ts: score?: number),
but the runtime validator in HFInferenceImageSegmentationTask only
accepted undefined or a number, rejecting null with:

  API Implementation Error: TypeError: Invalid output: output must be
  of type Array<{label:string; score:number; mask: string}>

Several HF-Inference models (e.g. jonathandinu/face-parsing) return
null for score on per-segment masks, which is consistent with the
optional schema field but tripped the strict type guard.

Allow null alongside undefined and number.

Refs huggingface#1430
@Sanjays2402 Sanjays2402 force-pushed the fix/image-segmentation-null-score branch from 2c5934d to ad3b9ca Compare May 27, 2026 09:38
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.

Issue with image segmentation on HF inference provider

1 participant