Skip to content

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

fix(anthropic): support struct json schema in tool parameters (#21638)

Sanderhoff-altd2c052b19cad
diff --git a/internal/agent/component/agent_test.go b/internal/agent/component/agent_test.go
index 0a08689c0c..bf3fd360cf 100644
--- a/internal/agent/component/agent_test.go
+++ b/internal/agent/component/agent_test.go
@@ -15,9 +15,12 @@ package component
import (
"context"
"database/sql"
+ "encoding/json"
"errors"
"fmt"
"io"
+ "net/http"
+ "net/http/httptest"
"strings"
"testing"
"time"
@@ -28,9 +31,12 @@ import (
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
+ "gorm.io/gorm"
"ragflow/internal/agent/runtime"
agenttool "ragflow/internal/agent/tool"
+ "ragflow/internal/common"
+ "ragflow/internal/entity/models"
)
// withAgentRunner replaces the package-level agentRunner for the duration
@@ -1446,3 +1452,141 @@ func TestAgentDisabledTemperatureAndTopP(t *testing.T) {
}
}
}
+
+type fakeAgentRetrievalService struct {
+ chunks []agenttool.RetrievalChunk
+}
+
+func (f *fakeAgentRetrievalService) Search(ctx context.Context, db *gorm.DB, req agenttool.RetrievalRequest) ([]agenttool.RetrievalChunk, error) {
+ return f.chunks, nil
+}
+
+func TestAgent_AnthropicReActToolCallLoop(t *testing.T) {
+ common.AllowAnyHostForTest = true
+ defer func() { common.AllowAnyHostForTest = false }()
+
+ fakeRetrieval := &fakeAgentRetrievalService{
+ chunks: []agenttool.RetrievalChunk{
+ {ID: "c1", Content: "RAGFlow is an open-source RAG engine based on deep document understanding."},
+ },
+ }
+ prevSvc := agenttool.GetRetrievalService()
+ agenttool.SetRetrievalService(fakeRetrieval)
+ defer agenttool.SetRetrievalService(prevSvc)
+
+ if err := models.InitProviderManager("../../../conf/models"); err != nil {
+ t.Fatal(err)
+ }
+
+ turn := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ turn++
+ body, _ := io.ReadAll(r.Body)
+
+ if turn == 1 {
+ var req map[string]any
+ if err := json.Unmarshal(body, &req); err != nil {
+ t.Errorf("turn 1: unmarshal request body: %v", err)
+ }
+ tools, _ := req["tools"].([]any)
+ if len(tools) == 0 {
+ t.Errorf("turn 1: expected tools in request, got none")
+ } else {
+ t0, _ := tools[0].(map[string]any)
+ if t0["name"] != "search_my_dateset" {
+ t.Errorf("turn 1: expected tool name search_my_dateset, got %v", t0["name"])
+ }
+ schema, _ := t0["input_schema"].(map[string]any)
+ if schema == nil || schema["type"] != "object" {
+ t.Errorf("turn 1: expected object input_schema, got %#v", t0["input_schema"])
+ }
+ props, _ := schema["properties"].(map[string]any)
+ if props == nil || props["query"] == nil {
+ t.Errorf("turn 1: expected query property in input_schema, got %#v", schema["properties"])
+ }
+ }
+
+ // First turn (non-streaming): model decides to call tool
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte(`{
+ "id": "msg_001",
+ "type": "message",
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_01",
+ "name": "search_my_dateset",
+ "input": {"query": "ragflow 是什么"}
+ }
+ ],
+ "stop_reason": "tool_use",
+ "usage": {"input_tokens": 50, "output_tokens": 20}
+ }`))
+ return
+ }
+
+ if turn == 2 {
+ if !strings.Contains(string(body), "RAGFlow is an open-source RAG engine based on deep document understanding.") {
+ t.Errorf("turn 2: expected retrieval chunk in request body, got %s", string(body))
+ }
+ }
+
+ // Second turn (streaming): model answers with final text
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.WriteHeader(http.StatusOK)
+ flusher, _ := w.(http.Flusher)
+
+ events := []string{
+ `event: message_start` + "\n" + `data: {"type": "message_start", "message": {"id": "msg_002", "type": "message", "role": "assistant", "content": [], "model": "qwen3.5-27b", "usage": {"input_tokens": 80, "output_tokens": 1}}}` + "\n\n",
+ `event: content_block_start` + "\n" + `data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}` + "\n\n",
+ `event: content_block_delta` + "\n" + `data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "RAGFlow 是一款基于深度文档理解的开源 RAG 引擎。"}}` + "\n\n",
+ `event: content_block_stop` + "\n" + `data: {"type": "content_block_stop", "index": 0}` + "\n\n",
+ `event: message_delta` + "\n" + `data: {"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {"output_tokens": 30}}` + "\n\n",
+ `event: message_stop` + "\n" + `data: {"type": "message_stop"}` + "\n\n",
+ }
+ for _, e := range events {
+ _, _ = w.Write([]byte(e))
+ if flusher != nil {
+ flusher.Flush()
+ }
+ }
+ }))
+ defer srv.Close()
+
+ rawJSON := `{
+ "llm_id": "qwen3.5-27b",
+ "driver": "Anthropic",
+ "api_key": "test-key",
+ "base_url": "` + srv.URL + `",
+ "max_rounds": 5,
+ "sys_prompt": "You are a test assistant.",
+ "prompts": [
+ {"role": "user", "content": "The user query is ragflow 是什么"}
+ ],
+ "tools": [
+ {
+ "component_name": "Retrieval",
+ "name": "Retrieval",
+ "params": {
+ "dataset_ids": ["8d4eec40b50548cab31d4985bd807e23"]
+ }
+ }
+ ]
+ }`
+
+ var params map[string]any
+ if err := json.Unmarshal([]byte(rawJSON), &params); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+
+ p := agentParamFromMap(params)
+ msg, err := runEinoReActAgent(context.Background(), p)
+ if err != nil {
+ t.Fatalf("runEinoReActAgent failed: %v", err)
+ }
+ if msg.Content != "RAGFlow 是一款基于深度文档理解的开源 RAG 引擎。" {
+ t.Fatalf("unexpected content: %s", msg.Content)
+ }
+}
diff --git a/internal/entity/models/anthropic.go b/internal/entity/models/anthropic.go
index 22ab46b7aa..3e5eb26f4d 100644
--- a/internal/entity/models/anthropic.go
+++ b/internal/entity/models/anthropic.go
@@ -161,10 +161,26 @@ func applyAnthropicChatConfig(reqBody map[string]interface{}, chatModelConfig *C
}
inputSchema := map[string]any{"type": "object", "properties": map[string]any{}}
if parameters, present := function["parameters"]; present {
- inputSchema, ok = parameters.(map[string]any)
- if !ok || inputSchema == nil {
+ if parameters == nil {
return fmt.Errorf("anthropic: tool %q parameters must be an object", name)
}
+ switch p := parameters.(type) {
+ case map[string]any:
+ if p == nil {
+ return fmt.Errorf("anthropic: tool %q parameters must be an object", name)
+ }
+ inputSchema = p
+ default:
+ data, err := json.Marshal(p)
+ if err != nil {
+ return fmt.Errorf("anthropic: tool %q parameters cannot be marshaled: %w", name, err)
+ }
+ var m map[string]any
+ if err := json.Unmarshal(data, &m); err != nil || m == nil {
+ return fmt.Errorf("anthropic: tool %q parameters must be an object", name)
+ }
+ inputSchema = m
+ }
}
nativeTool := map[string]any{"name": name, "input_schema": inputSchema}
if description, ok := function["description"].(string); ok {
diff --git a/internal/entity/models/anthropic_test.go b/internal/entity/models/anthropic_test.go
index 1ac2f89d8d..9805dd683a 100644
--- a/internal/entity/models/anthropic_test.go
+++ b/internal/entity/models/anthropic_test.go
@@ -72,7 +72,11 @@ func TestAnthropicToolOptionalFields(t *testing.T) {
{"non-string description", map[string]any{"name": "clock", "parameters": map[string]any{"type": "object"}, "description": 42}, `{"input_schema":{"type":"object"},"name":"clock"}`, false},
{"null description", map[string]any{"name": "clock", "parameters": map[string]any{"type": "object"}, "description": nil}, `{"input_schema":{"type":"object"},"name":"clock"}`, false},
{"null parameters", map[string]any{"name": "clock", "parameters": nil}, "", true},
+ {"typed nil map parameters", map[string]any{"name": "clock", "parameters": (map[string]any)(nil)}, "", true},
{"non-object parameters", map[string]any{"name": "clock", "parameters": "invalid"}, "", true},
+ {"struct parameters", map[string]any{"name": "clock", "parameters": struct {
+ Type string `json:"type"`
+ }{Type: "object"}}, `{"input_schema":{"type":"object"},"name":"clock"}`, false},
} {
t.Run(tc.name, func(t *testing.T) {
body := make(map[string]any)