Skip to content

fix(dify): accept a bare string for the retrieval response query field - #3504

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/dify-query-string-or-object
Oct 27, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/dify-query-string-or-object

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT

Description

Fixes #1917

DifyResponse models the top-level query field of a knowledge base retrieval response as an object ({"content": "..."}), which is what self-hosted Dify returns. Dify Cloud (api.dify.ai) returns a bare string for the same field:

{"query": "ping", "records": [ { "segment": { ... }, "score": 0.9 } ]}

Jackson cannot bind a JSON string to DifyResponse.Query, so DifyRAGClient.retrieve throws JsonException: Failed to deserialize JSON to ...DifyResponse and every record is discarded: kb_search returns nothing against Dify Cloud, while stub responses shaped as an object hide the problem.

  • Add DifyQueryDeserializer, which maps both "query": "..." and "query": {"content": "..."} to DifyResponse.Query.
  • The field is informational -- the Dify integration never reads it (the only callers of DifyResponse#getQuery are the tests added here) -- so a shape that is neither a string nor an object yields a null query instead of failing the response. Losing the query text is harmless; losing all records is not.
  • DifyResponse.Query, getQuery()/setQuery() and the self-hosted shape are unchanged, so this is not a breaking change.
  • Document both shapes in the DifyResponse and DifyDocumentConverter javadoc.

Validation on commit 9797487:

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test) -- the affected module and its dependencies passed locally
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated -- no Dify response-shape documentation exists under docs/, so none was changed
  • Code is ready for review

Dify Cloud returns the top-level "query" field of a knowledge base
retrieval response as a bare string, while self-hosted Dify returns an
object. DifyResponse modelled it as an object only, so Jackson could not
bind the payload and threw, discarding every retrieved record and leaving
RAG retrieval empty.

Deserialize both shapes with a dedicated deserializer. The field is
informational -- the Dify integration never reads it -- so a shape that is
neither a string nor an object now yields a null query instead of failing
the whole response.

Fixes agentscope-ai#1917
@codecov

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.28571% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../integration/dify/model/DifyQueryDeserializer.java 64.28% 2 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@mars171

mars171 commented Oct 26, 2026

Copy link
Copy Markdown

补充一下实际使用情况,希望能推动这个 PR 的审核:

  1. 问题在 2.0.4 正式版仍然存在:DifyResponse 从 2.0.2 到 2.0.4 没有任何变化。请求 Dify 云端(api.dify.ai)的检索接口时,返回的 query 是字符串(如 "query": "ping"),反序列化整体失败,导致 kb_search 拿不到任何检索记录、知识库引用恒为空。自部署 Dify 返回的是对象形态,所以用对象格式的测试数据测不出这个问题。
  2. 这个 PR 的修复方向是对的:我们从 2.0.x 起在项目里用同包同名类覆盖了 DifyResponse,做了等价修复(在 Query 上增加兼容字符串的反序列化,对象格式保持原逻辑),并用单元测试覆盖了两种格式,一直在使用。本 PR 用自定义反序列化器同时兼容两种形态、遇到未知格式也不丢弃检索记录,处理得比我们的本地补丁更完整。
  3. 我们希望官方合并后能删除本地覆盖类、回到官方实现。辛苦维护者有空审核一下,如果能进入下一个补丁版本就更好了。

关联:#1917

@wahllllll

Copy link
Copy Markdown
Contributor Author

Thanks @mars171 — that matches what I reproduced against api.dify.ai: the cloud endpoint returns query as a plain string while self-hosted Dify returns an object, and this change accepts both without changing the object path.

Friendly bump for review — anything else needed from my side?

@jujn
jujn merged commit d08c40c into agentscope-ai:main Oct 27, 2026
6 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
#3504)

Backport the verified fix to the 2.0.x implementation and retain its
existing public contracts where the upstream kernel differs.

(cherry picked from commit d08c40c)
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
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] Dify Cloud 检索接口 query 字段返回字符串,导致 DifyResponse 反序列化失败(kb_search 对 api.dify.ai 引用恒为空)

3 participants