Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix: handle malformed provider and CLI responses (#21546)

buua436dbda628a850c
diff --git a/internal/cli/response.go b/internal/cli/response.go
index 8a5816d7bb..d869498124 100644
--- a/internal/cli/response.go
+++ b/internal/cli/response.go
@@ -177,6 +177,52 @@ func HandleCommonDataResponse(response *Response, command string) (ResponseIf, e
return &result, nil
}
+func printListResponse(data map[string]interface{}, totalKey, listKey string, format OutputFormat) {
+ total, ok := data[totalKey].(float64)
+ if !ok {
+ printListResponseError(totalKey, "a number")
+ return
+ }
+ rows, ok := responseRows(data, listKey)
+ if !ok {
+ return
+ }
+
+ fmt.Printf("Total: %0.0f\n", total)
+ PrintTableSimpleByFormat(rows, format)
+}
+
+func printResponseTable(data map[string]interface{}, listKey string, format OutputFormat) {
+ rows, ok := responseRows(data, listKey)
+ if !ok {
+ return
+ }
+ PrintTableSimpleByFormat(rows, format)
+}
+
+func responseRows(data map[string]interface{}, listKey string) ([]map[string]interface{}, bool) {
+ items, ok := data[listKey].([]interface{})
+ if !ok {
+ printListResponseError(listKey, "an array")
+ return nil, false
+ }
+ rows := make([]map[string]interface{}, 0, len(items))
+ for i, item := range items {
+ row, ok := item.(map[string]interface{})
+ if !ok {
+ printListResponseError(fmt.Sprintf("%s[%d]", listKey, i), "an object")
+ return nil, false
+ }
+ rows = append(rows, row)
+ }
+ return rows, true
+}
+
+func printListResponseError(field, expected string) {
+ fmt.Println("ERROR")
+ fmt.Printf("invalid response: %s must be %s\n", field, expected)
+}
+
type ListDocumentsResponse struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
@@ -199,14 +245,7 @@ func (r *ListDocumentsResponse) SetOutputFormat(format OutputFormat) {
func (r *ListDocumentsResponse) PrintOut() {
if r.Code == 0 {
- total := r.Data["total"].(float64)
- fmt.Printf("Total: %0.0f\n", total)
- docs := r.Data["docs"].([]interface{})
- table := make([]map[string]interface{}, 0)
- for _, doc := range docs {
- table = append(table, doc.(map[string]interface{}))
- }
- PrintTableSimpleByFormat(table, r.OutputFormat)
+ printListResponse(r.Data, "total", "docs", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
@@ -235,14 +274,7 @@ func (r *ListSyncLogsResponse) SetOutputFormat(format OutputFormat) {
func (r *ListSyncLogsResponse) PrintOut() {
if r.Code == 0 {
- total := r.Data["total"].(float64)
- fmt.Printf("Total: %0.0f\n", total)
- logs := r.Data["logs"].([]interface{})
- table := make([]map[string]interface{}, 0)
- for _, log := range logs {
- table = append(table, log.(map[string]interface{}))
- }
- PrintTableSimpleByFormat(table, r.OutputFormat)
+ printListResponse(r.Data, "total", "logs", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
@@ -271,14 +303,7 @@ func (r *ListAgentsResponse) SetOutputFormat(format OutputFormat) {
func (r *ListAgentsResponse) PrintOut() {
if r.Code == 0 {
- total := r.Data["total"].(float64)
- fmt.Printf("Total: %0.0f\n", total)
- docs := r.Data["canvas"].([]interface{})
- table := make([]map[string]interface{}, 0)
- for _, doc := range docs {
- table = append(table, doc.(map[string]interface{}))
- }
- PrintTableSimpleByFormat(table, r.OutputFormat)
+ printListResponse(r.Data, "total", "canvas", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
@@ -307,14 +332,7 @@ func (r *ListChatsResponse) SetOutputFormat(format OutputFormat) {
func (r *ListChatsResponse) PrintOut() {
if r.Code == 0 {
- total := r.Data["total"].(float64)
- fmt.Printf("Total: %0.0f\n", total)
- docs := r.Data["chats"].([]interface{})
- table := make([]map[string]interface{}, 0)
- for _, doc := range docs {
- table = append(table, doc.(map[string]interface{}))
- }
- PrintTableSimpleByFormat(table, r.OutputFormat)
+ printListResponse(r.Data, "total", "chats", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
@@ -343,14 +361,7 @@ func (r *ListSearchesResponse) SetOutputFormat(format OutputFormat) {
func (r *ListSearchesResponse) PrintOut() {
if r.Code == 0 {
- total := r.Data["total"].(float64)
- fmt.Printf("Total: %0.0f\n", total)
- docs := r.Data["search_apps"].([]interface{})
- table := make([]map[string]interface{}, 0)
- for _, doc := range docs {
- table = append(table, doc.(map[string]interface{}))
- }
- PrintTableSimpleByFormat(table, r.OutputFormat)
+ printListResponse(r.Data, "total", "search_apps", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
@@ -379,14 +390,7 @@ func (r *ListMemoriesResponse) SetOutputFormat(format OutputFormat) {
func (r *ListMemoriesResponse) PrintOut() {
if r.Code == 0 {
- total := r.Data["total_count"].(float64)
- fmt.Printf("Total: %0.0f\n", total)
- docs := r.Data["memory_list"].([]interface{})
- table := make([]map[string]interface{}, 0)
- for _, doc := range docs {
- table = append(table, doc.(map[string]interface{}))
- }
- PrintTableSimpleByFormat(table, r.OutputFormat)
+ printListResponse(r.Data, "total_count", "memory_list", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
@@ -802,12 +806,7 @@ func (r *TaskResponse) SetOutputFormat(format OutputFormat) {
func (r *TaskResponse) PrintOut() {
if r.Code == 0 {
- segmentsRaw := r.Data["segments"].([]interface{})
- segments := make([]map[string]interface{}, len(segmentsRaw))
- for i, v := range segmentsRaw {
- segments[i] = v.(map[string]interface{})
- }
- PrintTableSimpleByFormat(segments, r.OutputFormat)
+ printResponseTable(r.Data, "segments", r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
diff --git a/internal/cli/response_test.go b/internal/cli/response_test.go
new file mode 100644
index 0000000000..117d586c83
--- /dev/null
+++ b/internal/cli/response_test.go
@@ -0,0 +1,201 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package cli
+
+import (
+ "io"
+ "os"
+ "strings"
+ "testing"
+)
+
+func TestListResponsePrintOutHandlesMalformedData(t *testing.T) {
+ cases := []struct {
+ name string
+ totalKey string
+ listKey string
+ new func(map[string]interface{}) ResponseIf
+ }{
+ {
+ name: "documents", totalKey: "total", listKey: "docs",
+ new: func(data map[string]interface{}) ResponseIf { return &ListDocumentsResponse{Data: data} },
+ },
+ {
+ name: "sync logs", totalKey: "total", listKey: "logs",
+ new: func(data map[string]interface{}) ResponseIf { return &ListSyncLogsResponse{Data: data} },
+ },
+ {
+ name: "agents", totalKey: "total", listKey: "canvas",
+ new: func(data map[string]interface{}) ResponseIf { return &ListAgentsResponse{Data: data} },
+ },
+ {
+ name: "chats", totalKey: "total", listKey: "chats",
+ new: func(data map[string]interface{}) ResponseIf { return &ListChatsResponse{Data: data} },
+ },
+ {
+ name: "searches", totalKey: "total", listKey: "search_apps",
+ new: func(data map[string]interface{}) ResponseIf { return &ListSearchesResponse{Data: data} },
+ },
+ {
+ name: "memories", totalKey: "total_count", listKey: "memory_list",
+ new: func(data map[string]interface{}) ResponseIf { return &ListMemoriesResponse{Data: data} },
+ },
+ }
+ malformedData := []struct {
+ name string
+ make func(totalKey, listKey string) map[string]interface{}
+ }{
+ {
+ name: "missing total",
+ make: func(_, listKey string) map[string]interface{} {
+ return map[string]interface{}{listKey: []interface{}{}}
+ },
+ },
+ {
+ name: "total is not numeric",
+ make: func(totalKey, listKey string) map[string]interface{} {
+ return map[string]interface{}{totalKey: "many", listKey: []interface{}{}}
+ },
+ },
+ {
+ name: "list is not an array",
+ make: func(totalKey, listKey string) map[string]interface{} {
+ return map[string]interface{}{totalKey: float64(1), listKey: "not a list"}
+ },
+ },
+ {
+ name: "list item is not an object",
+ make: func(totalKey, listKey string) map[string]interface{} {
+ return map[string]interface{}{totalKey: float64(1), listKey: []interface{}{"not an object"}}
+ },
+ },
+ }
+
+ for _, responseCase := range cases {
+ for _, dataCase := range malformedData {
+ t.Run(responseCase.name+"/"+dataCase.name, func(t *testing.T) {
+ response := responseCase.new(dataCase.make(responseCase.totalKey, responseCase.listKey))
+ output, panicValue := capturePrintOut(t, response.PrintOut)
+ if panicValue != nil {
+ t.Fatalf("PrintOut panicked: %v", panicValue)
+ }
+ if !strings.Contains(output, "ERROR") {
+ t.Fatalf("PrintOut output = %q, want a malformed-response error", output)
+ }
+ })
+ }
+ }
+}
+
+func TestListResponsePrintOutDisplaysValidData(t *testing.T) {
+ cases := []struct {
+ name string
+ totalKey string
+ listKey string
+ new func(map[string]interface{}) ResponseIf
+ }{
+ {
+ name: "documents", totalKey: "total", listKey: "docs",
+ new: func(data map[string]interface{}) ResponseIf { return &ListDocumentsResponse{Data: data} },
+ },
+ {
+ name: "sync logs", totalKey: "total", listKey: "logs",
+ new: func(data map[string]interface{}) ResponseIf { return &ListSyncLogsResponse{Data: data} },
+ },
+ {
+ name: "agents", totalKey: "total", listKey: "canvas",
+ new: func(data map[string]interface{}) ResponseIf { return &ListAgentsResponse{Data: data} },
+ },
+ {
+ name: "chats", totalKey: "total", listKey: "chats",
+ new: func(data map[string]interface{}) ResponseIf { return &ListChatsResponse{Data: data} },
+ },
+ {
+ name: "searches", totalKey: "total", listKey: "search_apps",
+ new: func(data map[string]interface{}) ResponseIf { return &ListSearchesResponse{Data: data} },
+ },
+ {
+ name: "memories", totalKey: "total_count", listKey: "memory_list",
+ new: func(data map[string]interface{}) ResponseIf { return &ListMemoriesResponse{Data: data} },
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ data := map[string]interface{}{
+ tc.totalKey: float64(1),
+ tc.listKey: []interface{}{map[string]interface{}{"id": "item-1"}},
+ }
+ output, panicValue := capturePrintOut(t, tc.new(data).PrintOut)
+ if panicValue != nil {
+ t.Fatalf("PrintOut panicked: %v", panicValue)
+ }
+ if !strings.Contains(output, "Total: 1") || !strings.Contains(output, "item-1") {
+ t.Fatalf("PrintOut output = %q, want total and row", output)
+ }
+ })
+ }
+}
+
+func TestTaskResponsePrintOutHandlesMalformedSegments(t *testing.T) {
+ cases := []struct {
+ name string
+ data map[string]interface{}
+ }{
+ {name: "missing segments", data: map[string]interface{}{}},
+ {name: "segments is not an array", data: map[string]interface{}{"segments": "bad"}},
+ {name: "segment is not an object", data: map[string]interface{}{"segments": []interface{}{"bad"}}},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ output, panicValue := capturePrintOut(t, (&TaskResponse{Data: tc.data}).PrintOut)
+ if panicValue != nil {
+ t.Fatalf("PrintOut panicked: %v", panicValue)
+ }
+ if !strings.Contains(output, "ERROR") {
+ t.Fatalf("PrintOut output = %q, want malformed-response error", output)
+ }
+ })
+ }
+}
+
+func capturePrintOut(t *testing.T, print func()) (string, any) {
+ t.Helper()
+ original := os.Stdout
+ reader, writer, err := os.Pipe()
+ if err != nil {
+ t.Fatalf("create stdout pipe: %v", err)
+ }
+ os.Stdout = writer
+ var panicValue any
+ func() {
+ defer func() { panicValue = recover() }()
+ print()
+ }()
+ if err := writer.Close(); err != nil {
+ t.Fatalf("close stdout writer: %v", err)
+ }
+ os.Stdout = original
+ output, err := io.ReadAll(reader)
+ if err != nil {
+ t.Fatalf("read stdout capture: %v", err)
+ }
+ if err := reader.Close(); err != nil {
+ t.Fatalf("close stdout reader: %v", err)
+ }
+ return string(output), panicValue
+}
diff --git a/internal/entity/models/gitee.go b/internal/entity/models/gitee.go
index f8fa1d88c3..b9624e6f4e 100644
--- a/internal/entity/models/gitee.go
+++ b/internal/entity/models/gitee.go
@@ -705,15 +705,18 @@ func (g *GiteeModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[str
}
// Parse response
- var result map[string]interface{}
+ var result struct {
+ Balance *float64 `json:"balance"`
+ }
if err = json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
-
- balance := result["balance"].(float64)
+ if result.Balance == nil {
+ return nil, fmt.Errorf("invalid balance response: balance must be a number")
+ }
var response = map[string]interface{}{
- "balance": balance,
+ "balance": *result.Balance,
"currency": "CNY",
}
diff --git a/internal/entity/models/gitee_test.go b/internal/entity/models/gitee_test.go
index 07e7c0b69b..2e801ec8ac 100644
--- a/internal/entity/models/gitee_test.go
+++ b/internal/entity/models/gitee_test.go
@@ -50,6 +50,61 @@ func newGiteeForChatTest(baseURL string) *GiteeModel {
return NewGiteeModel(map[string]string{"default": baseURL}, URLSuffix{Chat: "chat/completions"})
}
+func TestGiteeBalanceRejectsMalformedBalanceWithoutPanicking(t *testing.T) {
+ withSSRFBypass(t)
+ cases := []struct {
+ name string
+ body string
+ }{
+ {name: "missing balance", body: `{}`},
+ {name: "null balance", body: `{"balance":null}`},
+ {name: "string balance", body: `{"balance":"10"}`},
+ {name: "object balance", body: `{"balance":{}}`},
+ }
+ apiKey := "test-key"
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = io.WriteString(w, tc.body)
+ }))
+ defer server.Close()
+
+ model := NewGiteeModel(map[string]string{"default": server.URL}, URLSuffix{Balance: "balance"})
+ var callErr error
+ var panicValue any
+ func() {
+ defer func() { panicValue = recover() }()
+ _, callErr = model.Balance(t.Context(), &APIConfig{ApiKey: &apiKey})
+ }()
+ if panicValue != nil {
+ t.Fatalf("Balance panicked for malformed response: %v", panicValue)
+ }
+ if callErr == nil {
+ t.Fatal("Balance error = nil, want malformed response error")
+ }
+ })
+ }
+}
+
+func TestGiteeBalanceParsesNumericBalance(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = io.WriteString(w, `{"balance":12.5}`)
+ }))
+ defer server.Close()
+
+ apiKey := "test-key"
+ model := NewGiteeModel(map[string]string{"default": server.URL}, URLSuffix{Balance: "balance"})
+ response, err := model.Balance(t.Context(), &APIConfig{ApiKey: &apiKey})
+ if err != nil {
+ t.Fatalf("Balance: %v", err)
+ }
+ if response["balance"] != 12.5 || response["currency"] != "CNY" {
+ t.Fatalf("Balance response = %#v, want balance 12.5 CNY", response)
+ }
+}
+
func TestGiteeStreamAcceptsTerminalWithoutDelta(t *testing.T) {
withSSRFBypass(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
diff --git a/internal/entity/models/jina.go b/internal/entity/models/jina.go
index 3e7c984be3..0d7c282913 100644
--- a/internal/entity/models/jina.go
+++ b/internal/entity/models/jina.go
@@ -647,16 +647,34 @@ func (j *JinaModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]Lis
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
- // Parse response
- var result map[string]interface{}
+ var result struct {
+ Data *[]json.RawMessage `json:"data"`
+ }
if err = json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
+ if result.Data == nil {
+ return nil, fmt.Errorf("invalid model list response: data must be an array")
+ }
- // convert result["data"] to []map[string]interface{}
- models := make([]ModelListItem, 0, len(result["data"].([]interface{})))
- for _, model := range result["data"].([]interface{}) {
- modelName := model.(map[string]interface{})["id"].(string)
+ models := make([]ModelListItem, 0, len(*result.Data))
+ for i, rawModel := range *result.Data {
+ var model map[string]json.RawMessage
+ if err := json.Unmarshal(rawModel, &model); err != nil || model == nil {
+ return nil, fmt.Errorf("invalid model list response: data[%d] must be a model object", i)
+ }
+ rawID, ok := model["id"]
+ if !ok {
+ return nil, fmt.Errorf("invalid model list response: data[%d].id is required", i)
+ }
+ var id string
+ if err := json.Unmarshal(rawID, &id); err != nil {
+ return nil, fmt.Errorf("invalid model list response: data[%d].id must be a string: %w", i, err)
+ }
+ modelName := strings.TrimSpace(id)
+ if modelName == "" {
+ return nil, fmt.Errorf("invalid model list response: data[%d].id must be a non-empty string", i)
+ }
models = append(models, ModelListItem{
ID: modelName,
OwnedBy: "",
diff --git a/internal/entity/models/jina_test.go b/internal/entity/models/jina_test.go
index 60df03e6af..19c58eade4 100644
--- a/internal/entity/models/jina_test.go
+++ b/internal/entity/models/jina_test.go
@@ -50,6 +50,58 @@ func newJinaForTest(baseURL string) *JinaModel {
)
}
+func TestJinaListModelsRejectsMalformedResponsesWithoutPanicking(t *testing.T) {
+ withSSRFBypass(t)
+ cases := []struct {
+ name string
+ body string
+ }{
+ {name: "missing data", body: `{}`},
+ {name: "data is not an array", body: `{"data":{}}`},
+ {name: "model is not an object", body: `{"data":[null]}`},
+ {name: "model id is missing", body: `{"data":[{}]}`},
+ {name: "model id is not a string", body: `{"data":[{"id":1}]}`},
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = io.WriteString(w, tc.body)
+ }))
+ defer server.Close()
+
+ var callErr error
+ var panicValue any
+ func() {
+ defer func() { panicValue = recover() }()
+ _, callErr = newJinaForTest(server.URL).ListModels(t.Context(), &APIConfig{})
+ }()
+ if panicValue != nil {
+ t.Fatalf("ListModels panicked for malformed response: %v", panicValue)
+ }
+ if callErr == nil {
+ t.Fatal("ListModels error = nil, want malformed response error")
+ }
+ })
+ }
+}
+
+func TestJinaListModelsParsesValidResponse(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = io.WriteString(w, `{"data":[{"id":"jina-embeddings-v3"}]}`)
+ }))
+ defer server.Close()
+
+ models, err := newJinaForTest(server.URL).ListModels(t.Context(), &APIConfig{})
+ if err != nil {
+ t.Fatalf("ListModels: %v", err)
+ }
+ if len(models) != 1 || models[0].Name != "jina-embeddings-v3" {
+ t.Fatalf("ListModels = %+v, want one jina-embeddings-v3 model", models)
+ }
+}
+
func TestJinaChatHappyPath(t *testing.T) {
withSSRFBypass(t)
ctx := t.Context()