mirror of
https://github.com/langgenius/dify-official-plugins.git
synced 2026-07-21 17:45:23 -04:00
Performance: Replace json with orjson in Function_Calling Agent Strategies for Faster Tool Invocation
#882
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @real-akimoto-akira on GitHub (Dec 16, 2025).
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.
Yes, when building an AI assistant that integrates multiple MCP tools (weather, calendar, email, database queries, etc.) for enterprise customers. During production deployment, we noticed that agent (a agent node with Dify Official Agent Strategies function_calling.py ) response times were higher than expected.
We tested the same workflow with a third-party MCP plugin that uses
orjson, and the performance difference was dramatic.2. Additional context or comments
No response
3. Can you help us with this feature?
@dosubot[bot] commented on GitHub (Dec 16, 2025):
Hi @real-akimoto-akira! I'm Dosu and I’m helping the dify-official-plugins team.
You can improve tool invocation performance in
function_calling.pyby replacing the built-injsonmodule withorjsonfor serialization and deserialization. The key locations for this change are wherejson.dumpsandjson.loadsare used to handle tool call arguments and responses—these are hot paths for agent workflows and directly impact latency when many tool calls are involved. For example, see usages like:Source
How to update:
import jsonwithimport orjson.json.dumps(obj, ensure_ascii=False)toorjson.dumps(obj).decode("utf-8")(sinceorjson.dumpsreturns bytes, not a string).json.loads(s)toorjson.loads(s)(drop-in replacement).orjsonraisesorjson.JSONDecodeError(compatible withjson.JSONDecodeError).orjsonalways outputs UTF-8, so you don’t needensure_ascii=False.No existing PRs or issues address this optimization in agent strategies, so this would be a new contribution. Make sure to add
orjsonto your dependencies (e.g.,requirements.txtorpyproject.toml) if it’s not already present.Let me know if you want a more detailed code diff or have questions about edge cases!
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other