Error when using workflow as a tool for agent or workflow chatbot #20324

Closed
opened 2026-02-21 20:06:50 -05:00 by yindo · 1 comment
Owner

Originally created by @hungvipqt15 on GitHub (Nov 13, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.8.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

When I configured and made a workflow as a tool. I use this tool for type agent chatbot then pull this bug:
"tool invoke error: Instance <Account at 0x7b92b6e0fc80> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)"

Image Image

But when I run this workflow, it ran successfully.

✔️ Expected Behavior

I want to run this tool without bugs.

Actual Behavior

No response

Originally created by @hungvipqt15 on GitHub (Nov 13, 2025). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.8.0 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce When I configured and made a workflow as a tool. I use this tool for type agent chatbot then pull this bug: "tool invoke error: Instance <Account at 0x7b92b6e0fc80> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)" <img width="1534" height="464" alt="Image" src="https://github.com/user-attachments/assets/86431433-8d71-42b4-8fe2-c7132c96992a" /> <img width="847" height="347" alt="Image" src="https://github.com/user-attachments/assets/1528e9b9-5125-43d9-96f8-1608c55927f9" /> But when I run this workflow, it ran successfully. ### ✔️ Expected Behavior I want to run this tool without bugs. ### ❌ Actual Behavior _No response_
yindo added the 🐞 bug label 2026-02-21 20:06:50 -05:00
yindo closed this issue 2026-02-21 20:06:50 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 13, 2025):

This is a known regression in Dify v1.8.0: when you invoke a workflow as a tool for an agent chatbot, SQLAlchemy ORM objects like Account can become detached from their session, causing the "Instance is not bound to a Session" error. This happens because agent-tool invocation often serializes/deserializes data or crosses async boundaries, breaking the session context, while direct workflow execution keeps objects session-bound throughout the run.

How to fix/work around this in v1.8.0:

  • Instead of passing ORM objects (like Account) between workflow/tool boundaries, pass only their IDs (or other primitive data).
  • Inside the tool or workflow node, re-query the object using a fresh session context. For example:
from sqlalchemy.orm import Session
with Session(db.engine) as session:
    account = session.query(Account).get(account_id)
    # use account safely here

If you must use an ORM object after the session is closed, re-attach it with db.session.merge(account) before accessing its attributes.

Why does direct workflow execution work?
Direct runs keep the session alive for the duration, so objects remain bound. Agent-tool calls break this context, so you need to re-query or re-attach objects.

Permanent fix:
This issue is resolved in the main branch after v1.8.0 by refactoring session management to always use context-managed sessions for queries. The official fix is in PR #24656 and will be included in a future release. Upgrading to the latest version is recommended once available [source].

Temporary workarounds:

  • Restarting your Docker container may help for legacy tools, but is not a general fix.
  • For advanced users, you can expunge objects from the session before returning them, or (hacky) modify extract_tenant_id to return a static ID if you only use one tenant [source].

For more details and code examples, see this issue thread and session management PR.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Nov 13, 2025): <!-- Answer --> This is a known regression in Dify v1.8.0: when you invoke a workflow as a tool for an agent chatbot, SQLAlchemy ORM objects like `Account` can become detached from their session, causing the "Instance is not bound to a Session" error. This happens because agent-tool invocation often serializes/deserializes data or crosses async boundaries, breaking the session context, while direct workflow execution keeps objects session-bound throughout the run. **How to fix/work around this in v1.8.0:** - Instead of passing ORM objects (like `Account`) between workflow/tool boundaries, pass only their IDs (or other primitive data). - Inside the tool or workflow node, re-query the object using a fresh session context. For example: ```python from sqlalchemy.orm import Session with Session(db.engine) as session: account = session.query(Account).get(account_id) # use account safely here ``` If you must use an ORM object after the session is closed, re-attach it with `db.session.merge(account)` before accessing its attributes. **Why does direct workflow execution work?** Direct runs keep the session alive for the duration, so objects remain bound. Agent-tool calls break this context, so you need to re-query or re-attach objects. **Permanent fix:** This issue is resolved in the main branch after v1.8.0 by refactoring session management to always use context-managed sessions for queries. The official fix is in [PR #24656](https://github.com/langgenius/dify/pull/24656) and will be included in a future release. Upgrading to the latest version is recommended once available [[source]](https://github.com/langgenius/dify/issues/25412). **Temporary workarounds:** - Restarting your Docker container may help for legacy tools, but is not a general fix. - For advanced users, you can expunge objects from the session before returning them, or (hacky) modify `extract_tenant_id` to return a static ID if you only use one tenant [[source]](https://github.com/langgenius/dify/issues/25412). For more details and code examples, see [this issue thread](https://github.com/langgenius/dify/issues/24702) and [session management PR](https://github.com/langgenius/dify/pull/24656). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/09544e78-70e0-46cb-8efd-fe170e77ec24?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/28213)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#20324