API Tool returns Python string representation instead of JSON for array responses #19592

Closed
opened 2026-02-21 20:01:39 -05:00 by yindo · 0 comments
Owner

Originally created by @QuantumGhost on GitHub (Oct 19, 2025).

Originally assigned to: @QuantumGhost on GitHub.

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

main

Cloud or Self Hosted

Cloud

Steps to reproduce

When using custom API tools, if the upstream API returns a JSON array response, the text field contains the Python string representation of the array (e.g., "[{'id': 1}, {'id': 2}]") instead of proper JSON serialization (e.g., "[{\"id\": 1}, {\"id\": 2}]"). This causes invalid JSON that can't be properly parsed.

Background

PR #23456 was created to refactor response validation and parsing in the ApiTool class. However,
it accidentally made the text field empty for JSON responses.

PR #25973 fixed the empty string issue. However, the fix is incomplete - it only works correctly for JSON object
responses. For other JSON response types (such as arrays, strings, or numbers), the condition
isinstance(parsed_response.content, dict) evaluates to False, causing the else branch to execute.
As a result, JSON array responses are converted using Python's str() method instead of proper JSON serialization.

✔️ Expected Behavior

The ApiTool._invoke method should yield TextMessage with content: [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]

Actual Behavior

The ApiTool._invoke method yields TextMessage with content: [{'id': 1, 'name': 'Item 1'}, {'id': 2, 'name': 'Item 2'}].

Originally created by @QuantumGhost on GitHub (Oct 19, 2025). Originally assigned to: @QuantumGhost on GitHub. ### 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 main ### Cloud or Self Hosted Cloud ### Steps to reproduce When using custom API tools, if the upstream API returns a JSON array response, the text field contains the Python string representation of the array (e.g., `"[{'id': 1}, {'id': 2}]"`) instead of proper JSON serialization (e.g., `"[{\"id\": 1}, {\"id\": 2}]"`). This causes invalid JSON that can't be properly parsed. #### Background PR #23456 was created to refactor response validation and parsing in the `ApiTool` class. However, it accidentally made the `text` field empty for JSON responses. PR #25973 fixed the empty string issue. However, the fix is incomplete - it only works correctly for JSON object responses. For other JSON response types (such as arrays, strings, or numbers), the condition `isinstance(parsed_response.content, dict)` evaluates to `False`, causing the else branch to execute. As a result, JSON array responses are converted using Python's `str()` method instead of proper JSON serialization. ### ✔️ Expected Behavior The `ApiTool._invoke` method should yield `TextMessage` with content: `[{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]` ### ❌ Actual Behavior The `ApiTool._invoke` method yields `TextMessage` with content: `[{'id': 1, 'name': 'Item 1'}, {'id': 2, 'name': 'Item 2'}]`.
yindo added the 🐞 bugcloud labels 2026-02-21 20:01:39 -05:00
yindo closed this issue 2026-02-21 20:01:39 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#19592