Skip to content

fix(handler): return the execution trace when return_trace is set - #22326

Open
ccclucky wants to merge 1 commit into
infiniflow:mainfrom
ccclucky:fix/return-trace
Open

ccclucky wants to merge 1 commit into
infiniflow:mainfrom
ccclucky:fix/return-trace

Conversation

@ccclucky

Copy link
Copy Markdown
Contributor

Summary

return_trace is documented on POST /api/v1/agents/chat/completions and the Go handler parses it into agentChatCompletionsRequest.ReturnTrace, but nothing ever read the value, so the documented data.trace was never returned for non-streaming calls.

This mirrors the removed Python implementation (api/apps/restful_apis/agent_api.py): each node_finished event contributes {component_id, trace: [<node_finished data>]}, and the list is attached to the final answer when the flag is set. With the flag absent or false the response is unchanged (no trace key), so the change is additive.

The trace is also where the Agent component's tool_invocations live, so non-streaming callers can now read the agentic tool trace through the documented flag (Relates to #22306).

Fixes #22325

Changes

  • internal/handler/agent.go: collect node_finished data into traceItems when return_trace is true, and set data.trace on the aggregated response.
  • internal/handler/agent_test.go: TestAgentChatCompletions_NonStreamingReturnsTrace covers both return_trace: true (two node_finished events → two entries with component_id and the node data preserved) and return_trace absent (no trace key).

Verification

Run in the build sandbox (Linux, bash build.sh toolchain):

gofmt -l internal/handler/agent.go internal/handler/agent_test.go   # no output
bash build.sh --test ./internal/handler/ -count=1
ok  	ragflow/internal/handler	3.391s

Red/green: reverting only the agent.go hunk makes the new test fail with response data has no trace, and it passes with the change.

`return_trace` is documented on POST /api/v1/agents/chat/completions and
the request struct parsed it, but no code read the value, so the
aggregated response never carried the documented `data.trace`. Mirror the
Python agent_api.py aggregation: each `node_finished` event contributes
{component_id, trace: [data]} and the list is attached to the final
answer when the flag is set.
@ccclucky

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The non-streaming agent chat handler now collects node_finished event data when return_trace is enabled. It adds collected entries to the response as data.trace. A test checks the response with the flag enabled and disabled.

Changes: Non-streaming agent trace responses

Layer / File(s) Summary
Collect and return trace entries
internal/handler/agent.go, internal/handler/agent_test.go
When return_trace is enabled, the handler collects each node_finished event’s component_id and data, then adds non-empty entries to data.trace. The test checks the trace entries and verifies that the key is absent when the flag is disabled.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue #22306 requests that non-streaming chat responses expose agent trace data, including tool_invocations. This change adds non-streaming trace data from node_finished events.

Suggested reviewers: wangq8, 6ba3i

Merge Risk: Minimal · up to 79337

Non-streaming agent chat responses now include the execution trace when requested, and responses without the flag are unchanged. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the handler now returns the execution trace when return_trace is set. The raw summary confirms the change in internal/handler/agent.go, and the objectives confirm non-…
Description check Passed The description follows the repository template with a Summary section. It explains the background: return_trace is documented and parsed but never read, so data.trace was never returned for non-strea…
Linked Issues check Passed Issue #22325 requires that return_trace=true add data.data.trace to the non-streaming POST /api/v1/agents/chat/completions response, with one {component_id, trace: [node_finished data]} entry per fini…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches only internal/handler/agent.go and internal/handler/agent_test.go. The agent.go changes are the trace collection, the trace assignment, and a comment t…
Full details: Title check

Explanation

The title names the main change: the handler now returns the execution trace when return_trace is set. The raw summary confirms the change in internal/handler/agent.go, and the objectives confirm non-streaming responses now include data.trace. The fix(handler) prefix is a conventional-commit style and does not affect the assessment.

Full details: Description check

Explanation

The description follows the repository template with a Summary section. It explains the background: return_trace is documented and parsed but never read, so data.trace was never returned for non-streaming calls. It also states the change, the backward-compatible behavior when the flag is absent, the linked issue (#22325), the files changed, and verification steps with test results. The template asks only for a summary, so the description is complete.

Full details: Linked Issues check

Explanation

Issue #22325 requires that return_trace=true add data.data.trace to the non-streaming POST /api/v1/agents/chat/completions response, with one {component_id, trace: [node_finished data]} entry per finished node, and that the response stay unchanged when the flag is absent or false. The diff to internal/handler/agent.go implements this. It declares traceItems, and inside the node_finished branch it appends map{component_id: evData["component_id"], trace: []any{evData}} only when req.ReturnTrace is true. After aggregation it sets ansData["trace"] only when len(traceItems) > 0, so the key is absent when the flag is off. The added test TestAgentChatCompletions_NonStreamingReturnsTrace in internal/handler/agent_test.go covers two node_finished events (component_id, node data, and nested outputs preserved) and the disabled case where no trace key appears. The author reports the test passes and that it fails without the agent.go hunk. #22306 is context only and is not a separate requirement. No coding requirement remains unmet.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches only internal/handler/agent.go and internal/handler/agent_test.go. The agent.go changes are the trace collection, the trace assignment, and a comment that documents the field. The test changes cover the same behavior. No unrelated behavior change appears in the reviewed hunks. The ignored_files list is empty, so no evidence was excluded.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit peeks at traces in the flow,
Where finished nodes leave facts to show.
With return_trace set just right,
Their paths appear in the reply,
Then hops away beneath the moonlight.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9fbb7a69-0616-4dbb-bbf3-8c8cd111ca1d
Commits

Reviewing files that changed from the base of the PR and between 7d99beb and 79337e6.

Files selected for processing: 2

  • internal/handler/agent.go
  • internal/handler/agent_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@wangq8
wangq8 requested a review from JinHai-CN November 17, 2026 03:46
@yuzhichang
yuzhichang requested review from 6ba3i and removed request for JinHai-CN November 18, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: return_trace is accepted and documented but the Go agent chat completion endpoint ignores it

2 participants