fix: handle Llama 4 Scout array response shape in CF AI text decoder
Llama 4 Scout returns `result.response` as an array of objects
[{"generated_text":"..."}] instead of a plain string. Decode into
json.RawMessage and try both shapes; fall back to generated_text[0].
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -217,9 +217,11 @@ func (c *textGenHTTPClient) Generate(ctx context.Context, req TextRequest) (stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CF AI wraps responses: { "result": { "response": "..." }, "success": true }
|
// CF AI wraps responses: { "result": { "response": "..." }, "success": true }
|
||||||
|
// Some models (e.g. Llama 4 Scout) return response as an array:
|
||||||
|
// { "result": { "response": [{"generated_text":"..."}] } }
|
||||||
var wrapper struct {
|
var wrapper struct {
|
||||||
Result struct {
|
Result struct {
|
||||||
Response string `json:"response"`
|
Response json.RawMessage `json:"response"`
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
Errors []string `json:"errors"`
|
Errors []string `json:"errors"`
|
||||||
@@ -230,7 +232,19 @@ func (c *textGenHTTPClient) Generate(ctx context.Context, req TextRequest) (stri
|
|||||||
if !wrapper.Success {
|
if !wrapper.Success {
|
||||||
return "", fmt.Errorf("cfai/text: model %s error: %v", req.Model, wrapper.Errors)
|
return "", fmt.Errorf("cfai/text: model %s error: %v", req.Model, wrapper.Errors)
|
||||||
}
|
}
|
||||||
return wrapper.Result.Response, nil
|
// Try plain string first.
|
||||||
|
var text string
|
||||||
|
if err := json.Unmarshal(wrapper.Result.Response, &text); err == nil {
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
|
// Fall back: array of objects with a "generated_text" field.
|
||||||
|
var arr []struct {
|
||||||
|
GeneratedText string `json:"generated_text"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(wrapper.Result.Response, &arr); err == nil && len(arr) > 0 {
|
||||||
|
return arr[0].GeneratedText, nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("cfai/text: model %s: unrecognised response shape: %s", req.Model, wrapper.Result.Response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Models returns all supported text generation model metadata.
|
// Models returns all supported text generation model metadata.
|
||||||
|
|||||||
Reference in New Issue
Block a user