Skip to content

fix(adk): return modified message from AfterChatModel #717#792

Merged
hi-pender merged 1 commit intomainfrom
fix/after_model_state
Feb 14, 2026
Merged

fix(adk): return modified message from AfterChatModel #717#792
hi-pender merged 1 commit intomainfrom
fix/after_model_state

Conversation

@hi-pender
Copy link
Contributor

@hi-pender hi-pender commented Feb 14, 2026

Summary

Enable AfterChatModel middleware modifications to affect the ReAct Agent processing flow, while keeping AgentEvent consistent with ChatModel's original output.

Problem

In ReAct Agent scenario, when AfterChatModel callback modifies state.Messages (e.g., adding/removing ToolCalls), it should affect the subsequent processing flow (like whether to call tools), but the AgentEvent should still reflect the original ChatModel output.

Solution

NoTools Scenario (chatmodel.go)

  • AgentEvent is sent via callback with original ChatModel output
  • StatePostHandler returns st.Messages[len(st.Messages)-1] (the potentially modified message)

ReAct Scenario (react.go)

  • StatePostHandler returns st.Messages[len(st.Messages)-1] (the potentially modified message)
  • This affects subsequent flow - e.g., if AfterChatModel removes ToolCalls, the tool node won't be called
  • AgentEvent is still sent via callback with original ChatModel output
  • If user wants to emit custom events for modifications, they can call SendEvent method

Changes

react.go

  • Changed modelPostHandle return value from input to st.Messages[len(st.Messages)-1]
  • This ensures AfterChatModel modifications affect subsequent nodes (tools, etc.)

chatmodel.go

  • StatePostHandler returns original in (no change to output)
  • Restored callback-based event emission for NoTools scenario

chatmodel_test.go

Added test cases:

  • AfterChatModel_NoTools_ModifyDoesNotAffectEvent: Verifies NoTools scenario returns original ChatModel output
  • AfterChatModel_ReAct_ModifyAffectsFlow: Verifies removing ToolCalls in AfterChatModel stops tool execution
  • AfterChatModel_ReAct_AppendToolCall_AffectsFlow: Verifies adding ToolCalls in AfterChatModel triggers tool execution

Testing

All tests pass:

go test ./adk/...
ok      github.com/cloudwego/eino/adk
ok      github.com/cloudwego/eino/adk/filesystem
ok      github.com/cloudwego/eino/adk/middlewares/filesystem
ok      github.com/cloudwego/eino/adk/middlewares/reduction
ok      github.com/cloudwego/eino/adk/middlewares/skill
ok      github.com/cloudwego/eino/adk/prebuilt
ok      github.com/cloudwego/eino/adk/prebuilt/deep
ok      github.com/cloudwego/eino/adk/prebuilt/planexecute
ok      github.com/cloudwego/eino/adk/prebuilt/supervisor

@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.44%. Comparing base (e685d86) to head (88cf943).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #792      +/-   ##
==========================================
- Coverage   80.47%   80.44%   -0.04%     
==========================================
  Files         129      129              
  Lines       13047    13047              
==========================================
- Hits        10500    10496       -4     
- Misses       1745     1748       +3     
- Partials      802      803       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hi-pender hi-pender force-pushed the fix/after_model_state branch 2 times, most recently from 22dd885 to 293d9fd Compare February 14, 2026 09:12
@hi-pender hi-pender force-pushed the fix/after_model_state branch from 293d9fd to 88cf943 Compare February 14, 2026 09:16
@hi-pender hi-pender merged commit b5a282a into main Feb 14, 2026
18 of 19 checks passed
@hi-pender hi-pender deleted the fix/after_model_state branch February 14, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants