dify icon indicating copy to clipboard operation
dify copied to clipboard

messages api can't get the right result

Open zyh3826 opened this issue 5 months ago • 6 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing 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.7.2

Cloud or Self Hosted

Cloud, Self Hosted (Docker)

Steps to reproduce

  1. Build any chatbot, using its URL to start chatting on the web
  2. Using messages api to get chat history
  3. return {"code": "not_found", "message": "Conversation Not Exists.", "status": 404}
  4. Using chat-messages api to start chatting, messages api can get the right result.

✔️ Expected Behavior

messages api returns the right result

❌ Actual Behavior

messages api returns {"code": "not_found", "message": "Conversation Not Exists.", "status": 404}

zyh3826 avatar Sep 10 '25 09:09 zyh3826

Testing on the Dify cloud also gets the same result.

zyh3826 avatar Sep 10 '25 09:09 zyh3826

This behavior is expected based on how the APIs are designed. The messages API (GET /messages) will return a 404 "Conversation Not Exists" error if the conversation ID doesn't match an existing conversation for the current app, user, and source (api/console). It does not create conversations—only retrieves them. In contrast, the chat-messages API (POST /chat-messages) can create a new conversation if you don't provide a conversation ID, or append to an existing one if you do. That's why after starting a chat with chat-messages, the messages API can then retrieve the history: the conversation now exists in the correct context.

A common cause for the 404 is a mismatch in user/session/app context between when the conversation was created and when you try to retrieve it. Make sure you use the same authentication and context for both creating and retrieving conversations. For best results, always start conversations with chat-messages and then use messages to fetch history within the same session or API context. This applies to both cloud and self-hosted deployments, as the logic is the same across environments.

If you need more flexible retrieval or clearer errors, it could be worth suggesting a feature request for improved conversation lookup or error messaging in future releases.

References:

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] avatar Sep 10 '25 09:09 dosubot[bot]

https://github.com/langgenius/dify/blob/6453fc49735ed2e94de310d6a637fb29ca70aaac/api/services/message_service.py#L38-L42

Why are these two query parameters required? Can I get messages only with the conversation_id?

ymshenyu avatar Sep 19 '25 07:09 ymshenyu

Hi, @zyh3826. I'm Dosu, and I'm helping the Dify team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported a 404 "Conversation Not Exists" error when using the messages API to fetch chat history after starting a chat via a chatbot URL.
  • The messages API only retrieves existing conversations tied to the current app/user/session context and does not create new conversations.
  • The chat-messages API can create new conversations and thus works fine for starting chats and fetching history.
  • I advised ensuring consistent authentication and context when creating and retrieving conversations.
  • Another user questioned some query parameters in the message service code, but the core issue was clarified.

Next Steps:

  • Please confirm if this behavior is still relevant with the latest version of Dify; if so, feel free to keep the discussion open by commenting here.
  • Otherwise, this issue will be automatically closed in 15 days.

Thanks for your understanding and contribution!

dosubot[bot] avatar Oct 20 '25 16:10 dosubot[bot]

still relevant with the latest version of Dify

ymshenyu avatar Oct 22 '25 07:10 ymshenyu

Hi, @zyh3826. I'm Dosu, and I'm helping the Dify team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that in Dify v1.7.2, the messages API returns a 404 "Conversation Not Exists" error when fetching chat history after starting a chat via a chatbot URL.
  • Starting chats with the chat-messages API works correctly, as it creates new conversations.
  • The messages API only retrieves existing conversations tied to the current app/user/session context and does not create new ones.
  • Another user confirmed the issue remains relevant in the latest Dify version.
  • No resolution has been implemented yet.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of Dify by commenting on this issue.
  • If I do not hear back within 15 days, I will automatically close this issue.

Thank you for your understanding and contribution!

dosubot[bot] avatar Dec 24 '25 16:12 dosubot[bot]