Skip to content

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

Open
wahllllll wants to merge 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/dify-query-string-or-object
Open

wahllllll wants to merge 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!

This branch has not been deployed

No deployments
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 引用恒为空)

1 participant