Compare commits

...

1 Commits

Author SHA1 Message Date
root
5bc69ad9ce fix(cfai): handle Llama 4 Scout direct JSON array response in Generate()
All checks were successful
Release / Test backend (push) Successful in 6m5s
Release / Test UI (push) Successful in 1m44s
Release / Build and push images (push) Successful in 7m6s
Release / Deploy to prod (push) Successful in 3m47s
Release / Deploy to homelab (push) Successful in 8s
Release / Gitea Release (push) Successful in 44s
Llama 4 Scout returns the model output directly as a JSON array in the
'response' field rather than as a plain string or [{"generated_text":"..."}].
The old fallback parsed it as [{"generated_text":""}] (Go JSON ignores
unknown fields) and silently returned an empty string — causing chapter-names
jobs to finish with results:null in the payload despite items_done matching
items_total.

Fix: only accept the generated_text fallback when the value is non-empty;
otherwise return the raw JSON bytes as a string so callers (parseChapterTitlesJSON
etc.) can extract what they need.
2026-04-24 18:24:25 +05:00

View File

@@ -237,14 +237,18 @@ func (c *textGenHTTPClient) Generate(ctx context.Context, req TextRequest) (stri
if err := json.Unmarshal(wrapper.Result.Response, &text); err == nil {
return text, nil
}
// Fall back: array of objects with a "generated_text" field.
// Fall back: array of objects with a "generated_text" field
// (older CF AI models return [{"generated_text":"..."}]).
var arr []struct {
GeneratedText string `json:"generated_text"`
}
if err := json.Unmarshal(wrapper.Result.Response, &arr); err == nil && len(arr) > 0 {
if err := json.Unmarshal(wrapper.Result.Response, &arr); err == nil && len(arr) > 0 && arr[0].GeneratedText != "" {
return arr[0].GeneratedText, nil
}
return "", fmt.Errorf("cfai/text: model %s: unrecognised response shape: %s", req.Model, wrapper.Result.Response)
// Final fallback: model returned the result directly as a JSON value
// (e.g. Llama 4 Scout returns [{"number":1,"title":"..."},...] directly).
// Return the raw JSON bytes as a string so callers can parse it themselves.
return string(wrapper.Result.Response), nil
}
// Models returns all supported text generation model metadata.