Skip to content

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

fix(agent): honor Tavily raw content settings (#21422)

Sbaaoui Idriss77b249eb7aac
diff --git a/internal/agent/component/tool_component_test.go b/internal/agent/component/tool_component_test.go
index aedb17f2b4..4e31a15199 100644
--- a/internal/agent/component/tool_component_test.go
+++ b/internal/agent/component/tool_component_test.go
@@ -462,6 +462,14 @@ func TestToolBackedComponentTavilyIntegration(t *testing.T) {
serverCalls := 0
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
serverCalls++
+ var body map[string]any
+ if err := json.NewDecoder(request.Body).Decode(&body); err != nil {
+ t.Error(err)
+ return
+ }
+ if body["include_raw_content"] != true {
+ t.Errorf("include_raw_content = %v, want true", body["include_raw_content"])
+ }
writer.Header().Set("Content-Type", "application/json")
_, _ = writer.Write([]byte(`{"results":[{"title":"RAGFlow","url":"https://ragflow.io","raw_content":"RAG article","content":"fallback","score":0.8,"custom":"preserved"}]}`))
}))
@@ -486,7 +494,7 @@ func TestToolBackedComponentTavilyIntegration(t *testing.T) {
if serverCalls != 0 || len(empty["json"].([]any)) != 0 || empty["formalized_content"] != "" {
t.Fatalf("empty query result = %#v, server calls = %d", empty, serverCalls)
}
- out, err := component.Invoke(runtime.WithState(t.Context(), state), nil, map[string]any{"query": "ragflow", "api_key": "key"})
+ out, err := component.Invoke(runtime.WithState(t.Context(), state), nil, map[string]any{"query": "ragflow", "api_key": "key", "include_raw_content": true})
if err != nil {
t.Fatalf("Invoke: %v", err)
}
@@ -495,7 +503,7 @@ func TestToolBackedComponentTavilyIntegration(t *testing.T) {
t.Fatalf("formalized_content = %q", rendered)
}
results, ok := out["json"].([]any)
- if !ok || len(results) != 1 || results[0].(map[string]any)["custom"] != "preserved" {
+ if !ok || len(results) != 1 || results[0].(map[string]any)["custom"] != "preserved" || results[0].(map[string]any)["raw_content"] != "RAG article" {
t.Fatalf("raw json results = %#v", out["json"])
}
chunks := state.GetRetrievalChunks()
diff --git a/internal/agent/tool/tavily.go b/internal/agent/tool/tavily.go
index 7dc2930d87..57fd2ec8e5 100644
--- a/internal/agent/tool/tavily.go
+++ b/internal/agent/tool/tavily.go
@@ -318,9 +318,7 @@ func (t *TavilyTool) InvokableRun(ctx context.Context, argsJSON string, _ ...too
return tavilyErrJSON(fmt.Errorf("tavily: api_key is required (or set TAVILY_API_KEY)")), nil
}
- // Match the Python implementation: always disable images and raw content.
p.IncludeImages = false
- p.IncludeRawContent = false
body, _ := json.Marshal(tavilyRequestBody{
Query: p.Query,
diff --git a/internal/agent/tool/tavily_test.go b/internal/agent/tool/tavily_test.go
index 987a36b805..f419232584 100644
--- a/internal/agent/tool/tavily_test.go
+++ b/internal/agent/tool/tavily_test.go
@@ -18,6 +18,7 @@ package tool
import (
"encoding/json"
+ "fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -81,11 +82,8 @@ func TestTavily_BuildRequest(t *testing.T) {
if gotBody["search_depth"] != "advanced" {
t.Errorf("body.search_depth = %v, want advanced", gotBody["search_depth"])
}
- // include_raw_content and include_images are always forced to false
- // to match the Python implementation (avoids bloating the context
- // with base64 image data).
- if gotBody["include_raw_content"] != false || gotBody["include_images"] != false {
- t.Errorf("include flags = raw:%v images:%v, want false/false (forced)", gotBody["include_raw_content"], gotBody["include_images"])
+ if gotBody["include_raw_content"] != true || gotBody["include_images"] != false {
+ t.Errorf("include flags = raw:%v images:%v, want true/false", gotBody["include_raw_content"], gotBody["include_images"])
}
}
@@ -149,6 +147,39 @@ func TestTavily_RequiresAPIKey(t *testing.T) {
}
}
+func TestTavily_UpstreamErrors(t *testing.T) {
+ t.Parallel()
+ for _, tc := range []struct {
+ name string
+ status int
+ body string
+ want string
+ }{
+ {"unauthorized", http.StatusUnauthorized, `{}`, "upstream returned 401"},
+ {"invalid JSON", http.StatusOK, `{`, "decode response"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(tc.status)
+ _, _ = w.Write([]byte(tc.body))
+ }))
+ defer srv.Close()
+ helper := NewHTTPHelper().WithClient(&http.Client{Transport: rewriteHostTransport(srv.URL)})
+ out, err := NewTavilyToolWith(helper).InvokableRun(t.Context(), `{"query":"ragflow","api_key":"key","include_raw_content":true}`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var envelope tavilyEnvelope
+ if err := json.Unmarshal([]byte(out), &envelope); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(envelope.Error, tc.want) || len(envelope.Results) != 0 {
+ t.Fatalf("error envelope = %#v, want %q and no results", envelope, tc.want)
+ }
+ })
+ }
+}
+
func TestTavily_APIKeyFromEnv(t *testing.T) {
t.Parallel()
ctx := t.Context()
@@ -260,7 +291,7 @@ func TestTavily_PreservesRawResults(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
- _, _ = w.Write([]byte(`{"results":[{"url":"https://a.example","title":"A","content":"alpha","score":0.9,"custom":{"request_id":"kept"}}]}`))
+ _, _ = w.Write([]byte(`{"results":[{"url":"https://a.example","title":"A","content":"alpha","raw_content":"full article","score":0.9,"custom":{"request_id":"kept"}}]}`))
}))
defer srv.Close()
helper := NewHTTPHelper().WithClient(&http.Client{Transport: rewriteHostTransport(srv.URL)})
@@ -276,6 +307,9 @@ func TestTavily_PreservesRawResults(t *testing.T) {
if !ok || custom["request_id"] != "kept" {
t.Fatalf("raw result fields were lost: %#v", envelope.Results[0])
}
+ if envelope.Results[0]["raw_content"] != "full article" {
+ t.Fatalf("raw_content was lost: %#v", envelope.Results[0])
+ }
}
func TestTavily_BuildByNameUsesNodeDefaults(t *testing.T) {
@@ -331,7 +365,7 @@ func TestTavily_ExplicitFlagsOverrideNodeDefaults(t *testing.T) {
t.Fatalf("node default include_answer was not sent: %#v", gotBody)
}
if gotBody["include_raw_content"] != false || gotBody["include_images"] != false {
- t.Fatalf("include_raw_content/images should be forced false: %#v", gotBody)
+ t.Fatalf("include_raw_content/images should default to false: %#v", gotBody)
}
if _, err := tavily.InvokableRun(ctx, `{"query":"ragflow","include_answer":false}`); err != nil {
t.Fatalf("InvokableRun: %v", err)
@@ -341,6 +375,40 @@ func TestTavily_ExplicitFlagsOverrideNodeDefaults(t *testing.T) {
}
}
+func TestTavily_RawContentOverridesNodeDefaults(t *testing.T) {
+ t.Parallel()
+ for _, nodeDefault := range []bool{false, true} {
+ for _, override := range []string{"", `,"include_raw_content":false`, `,"include_raw_content":true`} {
+ t.Run(fmt.Sprintf("default=%t/override=%s", nodeDefault, override), func(t *testing.T) {
+ var gotBody tavilyRequestBody
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
+ t.Error(err)
+ }
+ _, _ = w.Write([]byte(`{"results":[]}`))
+ }))
+ defer srv.Close()
+ built, err := BuildByName("tavily", map[string]any{"api_key": "stored-key", "include_raw_content": nodeDefault})
+ if err != nil {
+ t.Fatal(err)
+ }
+ tavily := built.(*TavilyTool)
+ tavily.helper = NewHTTPHelper().WithClient(&http.Client{Transport: rewriteHostTransport(srv.URL)})
+ if _, err := tavily.InvokableRun(t.Context(), `{"query":"ragflow"`+override+`}`); err != nil {
+ t.Fatal(err)
+ }
+ want := nodeDefault
+ if override != "" {
+ want = strings.HasSuffix(override, "true")
+ }
+ if gotBody.IncludeRawContent != want {
+ t.Fatalf("include_raw_content = %t, want %t", gotBody.IncludeRawContent, want)
+ }
+ })
+ }
+ }
+}
+
func TestTavily_BuildByNameRejectsInvalidNodeDefaults(t *testing.T) {
t.Parallel()