[PR #5440] chore: add create_json_message api for tools #24928

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

Original Pull Request: https://github.com/langgenius/dify/pull/5440

State: closed
Merged: Yes


Description

ChangeList:

  • remove the duplicate _convert_tool_response_to_str method of api/core/agent/base_agent_runner.py and api/core/tools/tool/tool.py, which I search whole files and not found any file use it
  • add create_json_message api for the tools develop
  • use this api refactor the google search tool

Reason:
the origin create_text_message and create_link_message is not friendly for the workflow node. For example, the search result of google is hard to read and parse:

 { "text": "Artificial intelligence, in its broadest sense, is intelligence exhibited by machines, particularly computer systems.\ncontent: Artificial intelligence, or AI, is technology that enables computers and machines to simulate human intelligence and problem-solving capabilities.\nlink: https://www.ibm.com/topics/artificial-intelligence\ncontent: Artificial intelligence (AI) is the theory and development of computer systems capable of performing tasks that historically required human ...\nlink: https://www.coursera.org/articles/what-is-artificial-intelligence\ncontent: Artificial intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems. Read the full definition.\nlink: https://www.techtarget.com/searchenterpriseai/definition/AI-Artificial-Intelligence\ncontent: Artificial Intelligence (AI) works by simulating human intelligence through the use of algorithms, data, and computational power. The goal is to ...\nlink: https://meng.uic.edu/news-stories/ai-artificial-intelligence-what-is-the-definition-of-ai-and-how-does-ai-work/\ncontent: AI is a machine's ability to perform the cognitive functions we associate with human minds, such as perceiving, reasoning, learning, interacting ...\nlink: https://www.mckinsey.com/featured-insights/mckinsey-explainers/what-is-ai\ncontent: AI automates repetitive learning and discovery through data. Instead of automating manual tasks, AI performs frequent, high-volume, computerized tasks. And it ...\nlink: https://www.sas.com/en_us/insights/analytics/what-is-artificial-intelligence.html\ncontent: Artificial intelligence is “a technical and scientific field devoted to the engineered system that generates outputs such as content, forecasts, recommendations ...\nlink: https://www.iso.org/artificial-intelligence/what-is-ai\ncontent: AI which stands for Artificial intelligence is a technology that mimics human intelligence to perform tasks and can iteratively improve themselves based on ...\nlink: https://www.oracle.com/artificial-intelligence/what-is-ai/\ncontent: Artificial intelligence refers to computer systems that are capable of performing tasks traditionally associated with human intelligence — such as making ...\nlink: https://builtin.com/artificial-intelligence\ncontent: Artificial intelligence, the ability of a computer or computer-controlled robot to perform tasks commonly associated with intelligent beings ...\nlink: https://www.britannica.com/technology/artificial-intelligence", "files": [] }

Now change it to json format is helpful, and both human and LLM can read it.

{
  "text": "",
  "files": [
    
  ],
  "json": [
    {
      "title": "Artificial intelligence",
      "description": "Artificial intelligence, in its broadest sense, is intelligence exhibited by machines, particularly computer systems.",
      "organic_results": [
        {
          "title": "What is Artificial Intelligence (AI)?",
          "link": "https://www.ibm.com/topics/artificial-intelligence",
          "snippet": "Artificial intelligence, or AI, is technology that enables computers and machines to simulate human intelligence and problem-solving capabilities."
        },
        {
          "title": "What Is Artificial Intelligence? Definition, Uses, and Types",
          "link": "https://www.coursera.org/articles/what-is-artificial-intelligence",
          "snippet": "Artificial intelligence (AI) is the theory and development of computer systems capable of performing tasks that historically required human ..."
        },
        ...
      ]
    }
  ]
}

image-20240620150810144

Type of Change

  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement

How Has This Been Tested?

I test it locally in agent mode and workflow mode.

  • TODO

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/5440 **State:** closed **Merged:** Yes --- # Description ChangeList: - remove the duplicate `_convert_tool_response_to_str` method of `api/core/agent/base_agent_runner.py` and `api/core/tools/tool/tool.py`, which I search whole files and not found any file use it - add `create_json_message` api for the tools develop - use this api refactor the google search tool Reason: the origin `create_text_message` and `create_link_message` is not friendly for the workflow node. For example, the search result of google is hard to read and parse: ``` { "text": "Artificial intelligence, in its broadest sense, is intelligence exhibited by machines, particularly computer systems.\ncontent: Artificial intelligence, or AI, is technology that enables computers and machines to simulate human intelligence and problem-solving capabilities.\nlink: https://www.ibm.com/topics/artificial-intelligence\ncontent: Artificial intelligence (AI) is the theory and development of computer systems capable of performing tasks that historically required human ...\nlink: https://www.coursera.org/articles/what-is-artificial-intelligence\ncontent: Artificial intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems. Read the full definition.\nlink: https://www.techtarget.com/searchenterpriseai/definition/AI-Artificial-Intelligence\ncontent: Artificial Intelligence (AI) works by simulating human intelligence through the use of algorithms, data, and computational power. The goal is to ...\nlink: https://meng.uic.edu/news-stories/ai-artificial-intelligence-what-is-the-definition-of-ai-and-how-does-ai-work/\ncontent: AI is a machine's ability to perform the cognitive functions we associate with human minds, such as perceiving, reasoning, learning, interacting ...\nlink: https://www.mckinsey.com/featured-insights/mckinsey-explainers/what-is-ai\ncontent: AI automates repetitive learning and discovery through data. Instead of automating manual tasks, AI performs frequent, high-volume, computerized tasks. And it ...\nlink: https://www.sas.com/en_us/insights/analytics/what-is-artificial-intelligence.html\ncontent: Artificial intelligence is “a technical and scientific field devoted to the engineered system that generates outputs such as content, forecasts, recommendations ...\nlink: https://www.iso.org/artificial-intelligence/what-is-ai\ncontent: AI which stands for Artificial intelligence is a technology that mimics human intelligence to perform tasks and can iteratively improve themselves based on ...\nlink: https://www.oracle.com/artificial-intelligence/what-is-ai/\ncontent: Artificial intelligence refers to computer systems that are capable of performing tasks traditionally associated with human intelligence — such as making ...\nlink: https://builtin.com/artificial-intelligence\ncontent: Artificial intelligence, the ability of a computer or computer-controlled robot to perform tasks commonly associated with intelligent beings ...\nlink: https://www.britannica.com/technology/artificial-intelligence", "files": [] } ``` Now change it to json format is helpful, and both human and LLM can read it. ``` { "text": "", "files": [ ], "json": [ { "title": "Artificial intelligence", "description": "Artificial intelligence, in its broadest sense, is intelligence exhibited by machines, particularly computer systems.", "organic_results": [ { "title": "What is Artificial Intelligence (AI)?", "link": "https://www.ibm.com/topics/artificial-intelligence", "snippet": "Artificial intelligence, or AI, is technology that enables computers and machines to simulate human intelligence and problem-solving capabilities." }, { "title": "What Is Artificial Intelligence? Definition, Uses, and Types", "link": "https://www.coursera.org/articles/what-is-artificial-intelligence", "snippet": "Artificial intelligence (AI) is the theory and development of computer systems capable of performing tasks that historically required human ..." }, ... ] } ] } ``` ![image-20240620150810144](https://github.com/langgenius/dify/assets/25834719/751d44ec-fa6d-4fd7-9a23-f2504470ed7a) ## Type of Change - [x] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement # How Has This Been Tested? I test it locally in agent mode and workflow mode. - [ ] TODO # Suggested Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] My changes generate no new warnings - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods - [ ] `optional` I have made corresponding changes to the documentation - [ ] `optional` I have added tests that prove my fix is effective or that my feature works - [ ] `optional` New and existing unit tests pass locally with my changes
yindo added the pull-request label 2026-02-21 20:23:57 -05:00
yindo closed this issue 2026-02-21 20:23:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#24928