[PR #19578] [CLOSED] feat: Transform tool calls into proper messages #25253

Closed
opened 2026-04-20 05:50:48 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/19578
Author: @Oleg52
Created: 11/28/2025
Status: Closed

Base: devHead: feature/tools-system


📝 Commits (10+)

📊 Changes

4 files changed (+161 additions, -53 deletions)

View changed files

📝 backend/open_webui/env.py (+15 -0)
📝 backend/open_webui/utils/middleware.py (+15 -5)
📝 src/lib/components/chat/Chat.svelte (+73 -39)
📝 src/lib/utils/index.ts (+58 -9)

📄 Description

Pull Request Checklist

Before submitting, make sure you've checked the following:

  • Target branch: Verify that the pull request targets the dev branch. Not targeting the dev branch will lead to immediate closure of the PR.
  • Description: Provide a concise description of the changes made in this pull request down below.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: If necessary, update relevant documentation Open WebUI Docs like environment variables, the tutorials, or other documentation sources.
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Perform manual tests to verify the implemented fix/feature works as intended AND does not break any other functionality. Take this as an opportunity to make screenshots of the feature/fix and include it in the PR description.
  • Agentic AI Code: Confirm this Pull Request is not written by any AI Agent or has at least gone through additional human review AND manual testing. If any AI Agent is the co-author of this PR, it may lead to immediate closure of the PR.
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Title Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following:
    • feat: Introduces a new feature or enhancement to the codebase

Changelog Entry

Description

  • This PR refactors how the client side sends chat messages on the chat completion endpoint so tool calls are sent as structured messages instead of embedded details tag. This change affects only native function calling type.

Since server side already sends proper structure when it handles tool call requests, this change should not break anything.

Messages structure that is sent before change:

{
	"role": "assistant",
	"content": "sure! let me create that for you\n"{\"status\": \"success\"}"\ndone! I generated your cottage image"
}

Messages structure that is sent after change:

[
	{
		"role": "assistant",
		"content": "sure! let me create that for you",
		"tool_calls": [
			{
				"index": 0,
				"id": "call_abc123",
				"type": "function",
				"function": {
					"name": "generate_image",
					"arguments": "{\"prompt\":\"beautiful cottage\"}"
				}
			}
		]
	},
	{
		"role": "tool",
		"tool_call_id": "call_abc123",
		"content": "{\"status\": \"success\"}"
	},
	{
		"role": "assistant",
		"content": "done! I generated your cottage image"
	}
]

Messages structure that is sent after change when tool call had failed(attribute done=false):

[
	{
		"role": "assistant",
		"content": "sure! let me create that for you",
		"tool_calls": [
			{
				"index": 0,
				"id": "call_abc123",
				"type": "function",
				"function": {
					"name": "generate_image",
					"arguments": "{\"prompt\":\"beautiful cottage\"}"
				}
			}
		]
	},
	{
		"role": "tool",
		"tool_call_id": "call_abc123",
		"content": ""
	},
	{
		"role": "user",
		"content": "..."
	}
]

Why this matters: my MCP tool returns lengthy json string(around 30k tokens), with the current implementation tool result is sent as html encoded string and it results in drastic tokens count increase(up to 1.5M tokens!), also tool calling context is not passed to the model.

Added

This change also introduces 2 non-persistent environment variables:

  • ENABLE_WRAP_TOOL_RESULT - enables or disables tool result wrapping into results array.
  • TOOL_RESULT_INDENT_SIZE - sets indent count for json.dumps function when serializing tool result. If it's set to 0, compact json will be produced. In my case it saves ~14k tokens on a lengthy json.

Additional Information

I tested it with my private MCP server with both Default and Native function calling types. Single and multiple function calls are passed properly.

There's an open discussion #18622 that also addresses tool call messages structure, but it suggest database schema modification.

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/19578 **Author:** [@Oleg52](https://github.com/Oleg52) **Created:** 11/28/2025 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `feature/tools-system` --- ### 📝 Commits (10+) - [`fe6783c`](https://github.com/open-webui/open-webui/commit/fe6783c16699911c7be17392596d579333fb110c) Merge pull request #19030 from open-webui/dev - [`fc05e0a`](https://github.com/open-webui/open-webui/commit/fc05e0a6c5d39da60b603b4d520f800d6e36f748) Merge pull request #19405 from open-webui/dev - [`e3faec6`](https://github.com/open-webui/open-webui/commit/e3faec62c58e3a83d89aa3df539feacefa125e0c) Merge pull request #19416 from open-webui/dev - [`9899293`](https://github.com/open-webui/open-webui/commit/9899293f050ad50ae12024cbebee7e018acd851e) Merge pull request #19448 from open-webui/dev - [`140605e`](https://github.com/open-webui/open-webui/commit/140605e660b8186a7d5c79fb3be6ffb147a2f498) Merge pull request #19462 from open-webui/dev - [`fc68071`](https://github.com/open-webui/open-webui/commit/fc68071e1d0c2e63fd18127d24bbd811027dc729) transform tool calls into proper messages - [`96c7c94`](https://github.com/open-webui/open-webui/commit/96c7c948a1d3912c4bf49aef3971b6b38957bd73) add env to control tool execution result - [`d10a71c`](https://github.com/open-webui/open-webui/commit/d10a71c298631539417d5693e0758b0005dcd765) fix parseDoubleEncodedString - [`0422b5e`](https://github.com/open-webui/open-webui/commit/0422b5e29d91bcf4e401f7f1633fe2b4708ccf18) fix formatting - [`0790cc8`](https://github.com/open-webui/open-webui/commit/0790cc8ef48bc2f46da4e41d348599c7e6eab341) fix formatting ### 📊 Changes **4 files changed** (+161 additions, -53 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/env.py` (+15 -0) 📝 `backend/open_webui/utils/middleware.py` (+15 -5) 📝 `src/lib/components/chat/Chat.svelte` (+73 -39) 📝 `src/lib/utils/index.ts` (+58 -9) </details> ### 📄 Description # Pull Request Checklist **Before submitting, make sure you've checked the following:** - [x] **Target branch:** Verify that the pull request targets the `dev` branch. **Not targeting the `dev` branch will lead to immediate closure of the PR.** - [x] **Description:** Provide a concise description of the changes made in this pull request down below. - [x] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [x] **Documentation:** If necessary, update relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs) like environment variables, the tutorials, or other documentation sources. - [x] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation? - [x] **Testing:** Perform manual tests to **verify the implemented fix/feature works as intended AND does not break any other functionality**. Take this as an opportunity to **make screenshots of the feature/fix and include it in the PR description**. - [x] **Agentic AI Code:** Confirm this Pull Request is **not written by any AI Agent** or has at least **gone through additional human review AND manual testing**. If any AI Agent is the co-author of this PR, it may lead to immediate closure of the PR. - [x] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [x] **Title Prefix:** To clearly categorize this pull request, prefix the pull request title using one of the following: - **feat**: Introduces a new feature or enhancement to the codebase # Changelog Entry ### Description - This PR refactors how the client side sends chat messages on the chat completion endpoint so tool calls are sent as structured messages instead of embedded details tag. This change affects only native function calling type. Since server side already sends proper structure when it handles tool call requests, this change should not break anything. Messages structure that is sent before change: ```json { "role": "assistant", "content": "sure! let me create that for you\n&quot;{\&quot;status\&quot;: \&quot;success\&quot;}&quot;\ndone! I generated your cottage image" } ``` Messages structure that is sent after change: ```json [ { "role": "assistant", "content": "sure! let me create that for you", "tool_calls": [ { "index": 0, "id": "call_abc123", "type": "function", "function": { "name": "generate_image", "arguments": "{\"prompt\":\"beautiful cottage\"}" } } ] }, { "role": "tool", "tool_call_id": "call_abc123", "content": "{\"status\": \"success\"}" }, { "role": "assistant", "content": "done! I generated your cottage image" } ] ``` Messages structure that is sent after change when tool call had failed(attribute done=false): ```json [ { "role": "assistant", "content": "sure! let me create that for you", "tool_calls": [ { "index": 0, "id": "call_abc123", "type": "function", "function": { "name": "generate_image", "arguments": "{\"prompt\":\"beautiful cottage\"}" } } ] }, { "role": "tool", "tool_call_id": "call_abc123", "content": "" }, { "role": "user", "content": "..." } ] ``` Why this matters: my MCP tool returns lengthy json string(around 30k tokens), with the current implementation tool result is sent as html encoded string and it results in drastic tokens count increase(up to 1.5M tokens!), also tool calling context is not passed to the model. ### Added This change also introduces 2 non-persistent environment variables: - `ENABLE_WRAP_TOOL_RESULT` - enables or disables tool result wrapping into `results` array. - `TOOL_RESULT_INDENT_SIZE` - sets indent count for `json.dumps` function when serializing tool result. If it's set to 0, compact json will be produced. In my case it saves ~14k tokens on a lengthy json. --- ### Additional Information I tested it with my private MCP server with both Default and Native function calling types. Single and multiple function calls are passed properly. There's an open discussion [#18622](https://github.com/open-webui/open-webui/discussions/18622) that also addresses tool call messages structure, but it suggest database schema modification. ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-20 05:50:48 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#25253