[GH-ISSUE #23957] issue: Tool calling causing a repeat in messages when using pipeline #58792

Closed
opened 2026-05-05 23:58:31 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @jayteaftw on GitHub (Apr 21, 2026).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/23957

Check Existing Issues

  • I have searched for any existing and/or related issues.
  • I have searched for any existing and/or related discussions.
  • I have also searched in the CLOSED issues AND CLOSED discussions and found no related items (your issue might already be addressed on the development branch!).
  • I am using the latest version of Open WebUI.

Installation Method

Docker

Open WebUI Version

v8.12

Ollama Version (if applicable)

No response

Operating System

Mac OS

Browser (if applicable)

No response

Confirmation

  • I have read and followed all instructions in README.md.
  • I am using the latest version of both Open WebUI and Ollama.
  • I have included the browser console logs.
  • I have included the Docker container logs.
  • I have provided every relevant configuration, setting, and environment variable used in my setup.
  • I have clearly listed every relevant configuration, custom setting, environment variable, and command-line option that influences my setup (such as Docker Compose overrides, .env values, browser settings, authentication configurations, etc).
  • I have documented step-by-step reproduction instructions that are precise, sequential, and leave nothing to interpretation. My steps:
  • Start with the initial platform/version/OS and dependencies used,
  • Specify exact install/launch/configure commands,
  • List URLs visited, user input (incl. example values/emails/passwords if needed),
  • Describe all options and toggles enabled or changed,
  • Include any files or environmental changes,
  • Identify the expected and actual result at each stage,
  • Ensure any reasonably skilled user can follow and hit the same issue.

Expected Behavior

When running the pipeline I show below, I expect the Model to Produce the text "The weather is 22°C", show the tool call, and finally print "Done"

Actual Behavior

Instead it prints "The weather is 22°C. Done.", the tool call, and then multiple chat messages of "The weather is 22°C. Done."

Steps to Reproduce

Setup a pipeline

from typing import List, Union, Generator, Iterator


class Pipeline:
    class Valves(BaseModel):
        pass

    def __init__(self):
        self.valves = self.Valves()



    def pipe(
        self,
        user_message: str,
        model_id: str,
        messages: List[dict],
        body: dict,
    ) -> Union[str, Generator, Iterator]:

        yield {
            "choices": [{
                "delta": {"role": "assistant", "content": "The weather is 22°C. "},
                "finish_reason": None
            }]
        }

       
        yield {
            "choices": [{
                "delta": {
                    "tool_calls": [{
                        "index": 0,
                        "id": "call_123",
                        "type": "function",
                        "function": {"name": "get_weather_test", "arguments": '{"location": "SF"}'}
                    }]
                },
                "finish_reason": "tool_calls" 
            }]
        }


        yield {
            "choices": [{
                "delta": {"content": "Done."},
                "finish_reason": "stop"
            }]
        }

Logs & Screenshots

Image

Additional Information

What are the proper messages that a pipeline can retun/ stream?
For example, it mentions strings and generators in the documentation but yielding the following seems to be valid but is not mentioned in the documentation

{
    "choices": [{
           "delta": {
                 "content": "Done."},
                  "finish_reason": "stop"
            }]
        }
Originally created by @jayteaftw on GitHub (Apr 21, 2026). Original GitHub issue: https://github.com/open-webui/open-webui/issues/23957 ### Check Existing Issues - [x] I have searched for any existing and/or related issues. - [x] I have searched for any existing and/or related discussions. - [x] I have also searched in the CLOSED issues AND CLOSED discussions and found no related items (your issue might already be addressed on the development branch!). - [x] I am using the latest version of Open WebUI. ### Installation Method Docker ### Open WebUI Version v8.12 ### Ollama Version (if applicable) _No response_ ### Operating System Mac OS ### Browser (if applicable) _No response_ ### Confirmation - [x] I have read and followed all instructions in `README.md`. - [x] I am using the latest version of **both** Open WebUI and Ollama. - [x] I have included the browser console logs. - [x] I have included the Docker container logs. - [x] I have **provided every relevant configuration, setting, and environment variable used in my setup.** - [x] I have clearly **listed every relevant configuration, custom setting, environment variable, and command-line option that influences my setup** (such as Docker Compose overrides, .env values, browser settings, authentication configurations, etc). - [x] I have documented **step-by-step reproduction instructions that are precise, sequential, and leave nothing to interpretation**. My steps: - Start with the initial platform/version/OS and dependencies used, - Specify exact install/launch/configure commands, - List URLs visited, user input (incl. example values/emails/passwords if needed), - Describe all options and toggles enabled or changed, - Include any files or environmental changes, - Identify the expected and actual result at each stage, - Ensure any reasonably skilled user can follow and hit the same issue. ### Expected Behavior When running the pipeline I show below, I expect the Model to Produce the text "The weather is 22°C", show the tool call, and finally print "Done" ### Actual Behavior Instead it prints "The weather is 22°C. Done.", the tool call, and then multiple chat messages of "The weather is 22°C. Done." ### Steps to Reproduce Setup a pipeline ```python from typing import List, Union, Generator, Iterator class Pipeline: class Valves(BaseModel): pass def __init__(self): self.valves = self.Valves() def pipe( self, user_message: str, model_id: str, messages: List[dict], body: dict, ) -> Union[str, Generator, Iterator]: yield { "choices": [{ "delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None }] } yield { "choices": [{ "delta": { "tool_calls": [{ "index": 0, "id": "call_123", "type": "function", "function": {"name": "get_weather_test", "arguments": '{"location": "SF"}'} }] }, "finish_reason": "tool_calls" }] } yield { "choices": [{ "delta": {"content": "Done."}, "finish_reason": "stop" }] } ``` ### Logs & Screenshots <img width="454" height="1076" alt="Image" src="https://github.com/user-attachments/assets/70026834-3be0-47cb-b7bb-5ef2a55aa34a" /> ### Additional Information What are the proper messages that a pipeline can retun/ stream? For example, it mentions strings and generators in the documentation but yielding the following seems to be valid but is not mentioned in the documentation ```json { "choices": [{ "delta": { "content": "Done."}, "finish_reason": "stop" }] } ```
GiteaMirror added the bug label 2026-05-05 23:58:31 -05:00
Author
Owner

@Classic298 commented on GitHub (Apr 21, 2026):

can you share the code of the pipeline? might be a pipeline issue.

<!-- gh-comment-id:4290386880 --> @Classic298 commented on GitHub (Apr 21, 2026): can you share the code of the pipeline? might be a pipeline issue.
Author
Owner

@jayteaftw commented on GitHub (Apr 21, 2026):

@Classic298 this is what you are looking for? This is what I linked above and used to reproduce this error

from typing import List, Union, Generator, Iterator


class Pipeline:
    class Valves(BaseModel):
        pass

    def __init__(self):
        self.valves = self.Valves()



    def pipe(
        self,
        user_message: str,
        model_id: str,
        messages: List[dict],
        body: dict,
    ) -> Union[str, Generator, Iterator]:

        yield {
            "choices": [{
                "delta": {"role": "assistant", "content": "The weather is 22°C. "},
                "finish_reason": None
            }]
        }

       
        yield {
            "choices": [{
                "delta": {
                    "tool_calls": [{
                        "index": 0,
                        "id": "call_123",
                        "type": "function",
                        "function": {"name": "get_weather_test", "arguments": '{"location": "SF"}'}
                    }]
                },
                "finish_reason": "tool_calls" 
            }]
        }


        yield {
            "choices": [{
                "delta": {"content": "Done."},
                "finish_reason": "stop"
            }]
        }

<!-- gh-comment-id:4290519343 --> @jayteaftw commented on GitHub (Apr 21, 2026): @Classic298 this is what you are looking for? This is what I linked above and used to reproduce this error ```python from typing import List, Union, Generator, Iterator class Pipeline: class Valves(BaseModel): pass def __init__(self): self.valves = self.Valves() def pipe( self, user_message: str, model_id: str, messages: List[dict], body: dict, ) -> Union[str, Generator, Iterator]: yield { "choices": [{ "delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None }] } yield { "choices": [{ "delta": { "tool_calls": [{ "index": 0, "id": "call_123", "type": "function", "function": {"name": "get_weather_test", "arguments": '{"location": "SF"}'} }] }, "finish_reason": "tool_calls" }] } yield { "choices": [{ "delta": {"content": "Done."}, "finish_reason": "stop" }] } ```
Author
Owner

@Classic298 commented on GitHub (Apr 21, 2026):

Couple of things going on here:

1. You're on v0.8.12. Current is 0.9.x with a significantly reworked streaming path. Before we dig in, please reproduce on the latest build — there's a reasonable chance this is already addressed.

2. Your pipeline is emitting a non-spec-compliant stream. Per the OpenAI Chat Completions spec, finish_reason: "tool_calls" terminates the assistant turn. The expected flow is:

  • Assistant emits content + tool_calls → finish_reason: "tool_calls" → stream ends
  • Client (Open WebUI) executes the tool → appends tool result as a role: "tool" message → sends a new completion request
  • Assistant generates the final "Done." message in the next turn

What your pipeline does — yielding more content deltas after finish_reason: "tool_calls" — isn't defined behavior. The duplication you're seeing is Open WebUI's streaming handler reacting to a stream shape it doesn't expect: finish_reason: "tool_calls" triggers the tool-call handling path, but your stream keeps yielding, so the handler ends up processing the content twice.

Fix your pipeline: either

  • split it into two generators (first yields content+tool_calls and stops; after tool result comes back, second yields "Done."), or
  • don't set finish_reason: "tool_calls" in the middle — emit the tool_calls delta without finishing, continue with more content, then close with finish_reason: "stop" at the very end. (Note: this second pattern still isn't officially OpenAI-spec either, so behavior is best-effort.)

On your question about valid pipeline return types: str and generators/iterators yielding either raw text or OpenAI-compatible chunk dicts are both supported. The chunk-dict pattern is the one you're already using — it's fine as long as the stream follows normal OpenAI semantics (single finish_reason at the end of the turn).

<!-- gh-comment-id:4290618988 --> @Classic298 commented on GitHub (Apr 21, 2026): Couple of things going on here: **1. You're on v0.8.12.** Current is 0.9.x with a significantly reworked streaming path. Before we dig in, please reproduce on the latest build — there's a reasonable chance this is already addressed. **2. Your pipeline is emitting a non-spec-compliant stream.** Per the OpenAI Chat Completions spec, `finish_reason: "tool_calls"` *terminates* the assistant turn. The expected flow is: - Assistant emits content + tool_calls → `finish_reason: "tool_calls"` → stream ends - Client (Open WebUI) executes the tool → appends tool result as a `role: "tool"` message → sends a **new** completion request - Assistant generates the final "Done." message in the *next* turn What your pipeline does — yielding more `content` deltas *after* `finish_reason: "tool_calls"` — isn't defined behavior. **The duplication you're seeing is Open WebUI's streaming handler reacting to a stream shape it doesn't expect: `finish_reason: "tool_calls"` triggers the tool-call handling path, but your stream keeps yielding, so the handler ends up processing the content twice.** **Fix your pipeline:** either - split it into two generators (first yields content+tool_calls and stops; after tool result comes back, second yields "Done."), or - don't set `finish_reason: "tool_calls"` in the middle — emit the tool_calls delta without finishing, continue with more content, then close with `finish_reason: "stop"` at the very end. (Note: this second pattern still isn't officially OpenAI-spec either, so behavior is best-effort.) On your question about valid pipeline return types: `str` and generators/iterators yielding either raw text or OpenAI-compatible chunk dicts are both supported. The chunk-dict pattern is the one you're already using — it's fine as long as the stream follows normal OpenAI semantics (single `finish_reason` at the end of the turn).
Author
Owner

@jayteaftw commented on GitHub (Apr 21, 2026):

@Classic298 Hmm interesting,
I guess the bigger picture is I have a deep agent from langchain running in an open webui pipeline.
Since the tools are be executed by the agent and not the open webui client, how would you recommend then for me to be able to display the agents tool execution inside of open webui when it streams back the ToolMessage object?

for chunk in agent.stream(
    {"messages": messages}, stream_mode=["updates", "messages"], version="v2"
):
    if "messages" == chunk["type"]:
        message = chunk["data"][0]
        if isinstance(message, ToolMessage):
            yield  ### What should I yield here?
        elif isinstance(message, AIMessageChunk):
            yield chunk_to_openai_delta(message)

or how could I follow the suggested spec split it into two generators (first yields content+tool_calls and stops; after tool result comes back, second yields "Done.")?

<!-- gh-comment-id:4290687371 --> @jayteaftw commented on GitHub (Apr 21, 2026): @Classic298 Hmm interesting, I guess the bigger picture is I have a deep agent from langchain running in an open webui pipeline. Since the tools are be executed by the agent and not the open webui client, how would you recommend then for me to be able to display the agents tool execution inside of open webui when it streams back the ToolMessage object? ```python for chunk in agent.stream( {"messages": messages}, stream_mode=["updates", "messages"], version="v2" ): if "messages" == chunk["type"]: message = chunk["data"][0] if isinstance(message, ToolMessage): yield ### What should I yield here? elif isinstance(message, AIMessageChunk): yield chunk_to_openai_delta(message) ``` or how could I follow the suggested spec `split it into two generators (first yields content+tool_calls and stops; after tool result comes back, second yields "Done.")`?
Author
Owner

@Classic298 commented on GitHub (Apr 21, 2026):

Good context — if the LangChain agent is executing tools internally, you should not emit finish_reason: "tool_calls" at all. That signal tells the Open WebUI client "take over, execute the tool, then request a continuation" — but in your case the tool is already done by the time you're streaming. Emitting it makes OWUI try to do the tool-call dance on top of what your agent already did, which is the root of the duplication.

Two patterns that work well for self-contained agent pipelines:

  1. Native delta tool_calls, but no tool_calls finish_reason. Yield the tool_calls delta to get the UI chip rendered, then keep streaming content, and close with finish_reason: "stop" at the very end:
# intermediate content
yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]}

# tool call intent (UI shows the call) — no finish_reason here
yield {"choices": [{"delta": {"tool_calls": [{
    "index": 0, "id": "call_123", "type": "function",
    "function": {"name": "get_weather_test", "arguments": '{"location": "SF"}'}
}]}, "finish_reason": None}]}

# ...your agent executes get_weather_test internally...

# continue streaming content
yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]}

# only one finish_reason at the very end
yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}
  1. Embed tool execution as content using <details>. If you want the tool result visible too, wrap it in a <details> block inside the content stream. Open WebUI renders these as collapsible inline blocks:
yield_text("The weather is 22°C.\n\n")
yield_text(f"<details type=\"tool_calls\" done=\"true\">\n")
yield_text(f"<summary>Called get_weather_test</summary>\n")
yield_text(f"```json\n{tool_input}\n```\n")
yield_text(f"```json\n{tool_result}\n```\n")
yield_text(f"</details>\n\n")
yield_text("Done.")
# final chunk with finish_reason: "stop"

Mapping from your LangChain loop:

for chunk in agent.stream(...):
    if chunk["type"] == "messages":
        message = chunk["data"][0]
        if isinstance(message, AIMessageChunk):
            if message.tool_calls:
                # Yield tool_calls delta with NO finish_reason
                yield {"choices": [{"delta": {"tool_calls": [...]}, "finish_reason": None}]}
            if message.content:
                yield chunk_to_openai_delta(message)
        elif isinstance(message, ToolMessage):
            # Tool already executed by the agent — either skip (pattern 1)
            # or emit as content inside a <details> block (pattern 2)
            yield {"choices": [{"delta": {"content": f"\n[tool result: {message.content}]\n"}, "finish_reason": None}]}
# after the loop ends:
yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}

The key rule: when the agent owns tool execution, finish_reason appears exactly once, at the end, and it's "stop" — not "tool_calls".

<!-- gh-comment-id:4290833600 --> @Classic298 commented on GitHub (Apr 21, 2026): Good context — if the LangChain agent is executing tools internally, you should not emit finish_reason: "tool_calls" at all. That signal tells the Open WebUI client "take over, execute the tool, then request a continuation" — but in your case the tool is already done by the time you're streaming. Emitting it makes OWUI try to do the tool-call dance on top of what your agent already did, which is the root of the duplication. Two patterns that work well for self-contained agent pipelines: 1. Native delta tool_calls, but no tool_calls finish_reason. Yield the tool_calls delta to get the UI chip rendered, then keep streaming content, and close with finish_reason: "stop" at the very end: ``` # intermediate content yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]} # tool call intent (UI shows the call) — no finish_reason here yield {"choices": [{"delta": {"tool_calls": [{ "index": 0, "id": "call_123", "type": "function", "function": {"name": "get_weather_test", "arguments": '{"location": "SF"}'} }]}, "finish_reason": None}]} # ...your agent executes get_weather_test internally... # continue streaming content yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]} # only one finish_reason at the very end yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} ``` 2. Embed tool execution as content using `<details>`. If you want the tool result visible too, wrap it in a `<details>` block inside the content stream. Open WebUI renders these as collapsible inline blocks: ``` yield_text("The weather is 22°C.\n\n") yield_text(f"<details type=\"tool_calls\" done=\"true\">\n") yield_text(f"<summary>Called get_weather_test</summary>\n") yield_text(f"```json\n{tool_input}\n```\n") yield_text(f"```json\n{tool_result}\n```\n") yield_text(f"</details>\n\n") yield_text("Done.") # final chunk with finish_reason: "stop" ``` Mapping from your LangChain loop: ``` for chunk in agent.stream(...): if chunk["type"] == "messages": message = chunk["data"][0] if isinstance(message, AIMessageChunk): if message.tool_calls: # Yield tool_calls delta with NO finish_reason yield {"choices": [{"delta": {"tool_calls": [...]}, "finish_reason": None}]} if message.content: yield chunk_to_openai_delta(message) elif isinstance(message, ToolMessage): # Tool already executed by the agent — either skip (pattern 1) # or emit as content inside a <details> block (pattern 2) yield {"choices": [{"delta": {"content": f"\n[tool result: {message.content}]\n"}, "finish_reason": None}]} # after the loop ends: yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} ``` The key rule: when the agent owns tool execution, finish_reason appears exactly once, at the end, and it's "stop" — not "tool_calls".
Author
Owner

@jayteaftw commented on GitHub (Apr 21, 2026):

@Classic298 thanks for the information! So I updated to v0.9.1 used the 1st example as a test pipeline

from typing import List, Union, Generator, Iterator
from pydantic import BaseModel
from starlette.requests import Request


class Pipeline:
    class Valves(BaseModel):
        pass

    def __init__(self):
        self.valves = self.Valves()


    def pipe(
        self,
        user_message: str,
        model_id: str,
        messages: List[dict],
        body: dict,
    ) -> Union[str, Generator, Iterator]:



        # intermediate content
        yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]}

        # tool call intent (UI shows the call) — no finish_reason here
        yield {"choices": [{"delta": {"tool_calls": [{
            "index": 0, "id": "call_123", "type": "function",
            "function": {"name": "get_weather_test_v2", "arguments": '{"location": "SF"}'}
        }]}, "finish_reason": None}]}

        # ...your agent executes get_weather_test internally...

        # continue streaming content
        yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]}

        # only one finish_reason at the very end
        yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}

However I am getting similar results
Image

<!-- gh-comment-id:4291219595 --> @jayteaftw commented on GitHub (Apr 21, 2026): @Classic298 thanks for the information! So I updated to v0.9.1 used the 1st example as a test pipeline ```python from typing import List, Union, Generator, Iterator from pydantic import BaseModel from starlette.requests import Request class Pipeline: class Valves(BaseModel): pass def __init__(self): self.valves = self.Valves() def pipe( self, user_message: str, model_id: str, messages: List[dict], body: dict, ) -> Union[str, Generator, Iterator]: # intermediate content yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]} # tool call intent (UI shows the call) — no finish_reason here yield {"choices": [{"delta": {"tool_calls": [{ "index": 0, "id": "call_123", "type": "function", "function": {"name": "get_weather_test_v2", "arguments": '{"location": "SF"}'} }]}, "finish_reason": None}]} # ...your agent executes get_weather_test internally... # continue streaming content yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]} # only one finish_reason at the very end yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} ``` However I am getting similar results <img width="907" height="1089" alt="Image" src="https://github.com/user-attachments/assets/2f342741-ffe0-46f8-b44b-28b8b47002eb" />
Author
Owner

@Classic298 commented on GitHub (Apr 21, 2026):

My previous Pattern 1 advice was wrong, apologies for the runaround. Looking at backend/open_webui/utils/middleware.py, Open WebUI processes delta.tool_calls into its execution loop (around line 4369: while len(tool_calls) > 0 and tool_call_retries < CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES) regardless of finish_reason. Any delta.tool_calls the pipeline emits gets picked up, handed to the tool executor, and a continuation request is fired back at the same pipeline — which produces exactly the loop you're seeing (capped by CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES, ~30 iterations in your screenshot).

Correct approach for a self-contained agent: don't emit delta.tool_calls at all. Instead, emit the visualization as content using the same <details type="tool_calls"> block Open WebUI itself renders internally after tool execution (see middleware.py:498). The UI will show the exact same collapsible "View Result from ..." chip.

Minimal example:

import html, json

def pipe(self, user_message, model_id, messages, body):
    yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]}

    # Render tool execution WITHOUT using delta.tool_calls
    call_id = "call_123"
    name = "get_weather_test_v2"
    arguments = {"location": "SF"}
    result = {"temp_c": 22}

    details_block = (
        f'<details type="tool_calls" done="true" '
        f'id="{call_id}" name="{name}" '
        f'arguments="{html.escape(json.dumps(arguments))}">\n'
        f'<summary>Tool Executed</summary>\n'
        f'{html.escape(json.dumps(result, ensure_ascii=False))}\n'
        f'</details>\n'
    )
    yield {"choices": [{"delta": {"content": details_block}, "finish_reason": None}]}

    yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]}
    yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}

Mapped to your LangChain loop:

import html, json

for chunk in agent.stream({"messages": messages}, stream_mode=["updates", "messages"]):
    if chunk["type"] == "messages":
        message = chunk["data"][0]

        if isinstance(message, AIMessageChunk):
            # Stream content only — drop message.tool_calls entirely.
            if message.content:
                yield {"choices": [{"delta": {"content": message.content}, "finish_reason": None}]}

        elif isinstance(message, ToolMessage):
            details = (
                f'<details type="tool_calls" done="true" '
                f'id="{message.tool_call_id}" name="{message.name}" '
                f'arguments="{html.escape(json.dumps(getattr(message, "args", {}) or {}))}">\n'
                f'<summary>Tool Executed</summary>\n'
                f'{html.escape(json.dumps(message.content, ensure_ascii=False, default=str))}\n'
                f'</details>\n'
            )
            yield {"choices": [{"delta": {"content": details}, "finish_reason": None}]}

# final chunk
yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}

Rule of thumb: delta.tool_calls always means "please execute this for me, client." If your agent has already executed the tool, use content-embedded <details> blocks instead. That's exactly what Open WebUI's own server-side tool path emits after execution — you're producing the same output shape directly, skipping the execute-and-continue loop that doesn't apply when the agent owns tool execution.

<!-- gh-comment-id:4291247945 --> @Classic298 commented on GitHub (Apr 21, 2026): My previous Pattern 1 advice was wrong, apologies for the runaround. Looking at `backend/open_webui/utils/middleware.py`, Open WebUI processes `delta.tool_calls` into its execution loop (around line 4369: `while len(tool_calls) > 0 and tool_call_retries < CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES`) **regardless of `finish_reason`**. Any `delta.tool_calls` the pipeline emits gets picked up, handed to the tool executor, and a continuation request is fired back at the same pipeline — which produces exactly the loop you're seeing (capped by `CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES`, ~30 iterations in your screenshot). **Correct approach for a self-contained agent: don't emit `delta.tool_calls` at all.** Instead, emit the visualization as content using the same `<details type="tool_calls">` block Open WebUI itself renders internally after tool execution (see `middleware.py:498`). The UI will show the exact same collapsible "View Result from ..." chip. Minimal example: ```python import html, json def pipe(self, user_message, model_id, messages, body): yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]} # Render tool execution WITHOUT using delta.tool_calls call_id = "call_123" name = "get_weather_test_v2" arguments = {"location": "SF"} result = {"temp_c": 22} details_block = ( f'<details type="tool_calls" done="true" ' f'id="{call_id}" name="{name}" ' f'arguments="{html.escape(json.dumps(arguments))}">\n' f'<summary>Tool Executed</summary>\n' f'{html.escape(json.dumps(result, ensure_ascii=False))}\n' f'</details>\n' ) yield {"choices": [{"delta": {"content": details_block}, "finish_reason": None}]} yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]} yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} ``` Mapped to your LangChain loop: ```python import html, json for chunk in agent.stream({"messages": messages}, stream_mode=["updates", "messages"]): if chunk["type"] == "messages": message = chunk["data"][0] if isinstance(message, AIMessageChunk): # Stream content only — drop message.tool_calls entirely. if message.content: yield {"choices": [{"delta": {"content": message.content}, "finish_reason": None}]} elif isinstance(message, ToolMessage): details = ( f'<details type="tool_calls" done="true" ' f'id="{message.tool_call_id}" name="{message.name}" ' f'arguments="{html.escape(json.dumps(getattr(message, "args", {}) or {}))}">\n' f'<summary>Tool Executed</summary>\n' f'{html.escape(json.dumps(message.content, ensure_ascii=False, default=str))}\n' f'</details>\n' ) yield {"choices": [{"delta": {"content": details}, "finish_reason": None}]} # final chunk yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} ``` Rule of thumb: **`delta.tool_calls` always means "please execute this for me, client."** If your agent has already executed the tool, use content-embedded `<details>` blocks instead. That's exactly what Open WebUI's own server-side tool path emits after execution — you're producing the same output shape directly, skipping the execute-and-continue loop that doesn't apply when the agent owns tool execution.
Author
Owner

@jayteaftw commented on GitHub (Apr 22, 2026):

My previous Pattern 1 advice was wrong, apologies for the runaround. Looking at backend/open_webui/utils/middleware.py, Open WebUI processes delta.tool_calls into its execution loop (around line 4369: while len(tool_calls) > 0 and tool_call_retries < CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES) regardless of finish_reason. Any delta.tool_calls the pipeline emits gets picked up, handed to the tool executor, and a continuation request is fired back at the same pipeline — which produces exactly the loop you're seeing (capped by CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES, ~30 iterations in your screenshot).

Correct approach for a self-contained agent: don't emit delta.tool_calls at all. Instead, emit the visualization as content using the same <details type="tool_calls"> block Open WebUI itself renders internally after tool execution (see middleware.py:498). The UI will show the exact same collapsible "View Result from ..." chip.

Minimal example:

import html, json

def pipe(self, user_message, model_id, messages, body):
yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]}

# Render tool execution WITHOUT using delta.tool_calls
call_id = "call_123"
name = "get_weather_test_v2"
arguments = {"location": "SF"}
result = {"temp_c": 22}

details_block = (
    f'<details type="tool_calls" done="true" '
    f'id="{call_id}" name="{name}" '
    f'arguments="{html.escape(json.dumps(arguments))}">\n'
    f'<summary>Tool Executed</summary>\n'
    f'{html.escape(json.dumps(result, ensure_ascii=False))}\n'
    f'</details>\n'
)
yield {"choices": [{"delta": {"content": details_block}, "finish_reason": None}]}

yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]}
yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}

Mapped to your LangChain loop:

import html, json

for chunk in agent.stream({"messages": messages}, stream_mode=["updates", "messages"]):
if chunk["type"] == "messages":
message = chunk["data"][0]

    if isinstance(message, AIMessageChunk):
        # Stream content only — drop message.tool_calls entirely.
        if message.content:
            yield {"choices": [{"delta": {"content": message.content}, "finish_reason": None}]}

    elif isinstance(message, ToolMessage):
        details = (
            f'<details type="tool_calls" done="true" '
            f'id="{message.tool_call_id}" name="{message.name}" '
            f'arguments="{html.escape(json.dumps(getattr(message, "args", {}) or {}))}">\n'
            f'<summary>Tool Executed</summary>\n'
            f'{html.escape(json.dumps(message.content, ensure_ascii=False, default=str))}\n'
            f'</details>\n'
        )
        yield {"choices": [{"delta": {"content": details}, "finish_reason": None}]}

final chunk

yield {"choices": [{"delta": {}, "finish_reason": "stop"}]}
Rule of thumb: delta.tool_calls always means "please execute this for me, client." If your agent has already executed the tool, use content-embedded <details> blocks instead. That's exactly what Open WebUI's own server-side tool path emits after execution — you're producing the same output shape directly, skipping the execute-and-continue loop that doesn't apply when the agent owns tool execution.

Thank you! I dont know if this is a bug but yield {"choices": [{"delta": {"content": details}, "finish_reason": None}]} did not seem to work but yield details did

<!-- gh-comment-id:4297590333 --> @jayteaftw commented on GitHub (Apr 22, 2026): > My previous Pattern 1 advice was wrong, apologies for the runaround. Looking at `backend/open_webui/utils/middleware.py`, Open WebUI processes `delta.tool_calls` into its execution loop (around line 4369: `while len(tool_calls) > 0 and tool_call_retries < CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES`) **regardless of `finish_reason`**. Any `delta.tool_calls` the pipeline emits gets picked up, handed to the tool executor, and a continuation request is fired back at the same pipeline — which produces exactly the loop you're seeing (capped by `CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES`, ~30 iterations in your screenshot). > > **Correct approach for a self-contained agent: don't emit `delta.tool_calls` at all.** Instead, emit the visualization as content using the same `<details type="tool_calls">` block Open WebUI itself renders internally after tool execution (see `middleware.py:498`). The UI will show the exact same collapsible "View Result from ..." chip. > > Minimal example: > > import html, json > > def pipe(self, user_message, model_id, messages, body): > yield {"choices": [{"delta": {"role": "assistant", "content": "The weather is 22°C. "}, "finish_reason": None}]} > > # Render tool execution WITHOUT using delta.tool_calls > call_id = "call_123" > name = "get_weather_test_v2" > arguments = {"location": "SF"} > result = {"temp_c": 22} > > details_block = ( > f'<details type="tool_calls" done="true" ' > f'id="{call_id}" name="{name}" ' > f'arguments="{html.escape(json.dumps(arguments))}">\n' > f'<summary>Tool Executed</summary>\n' > f'{html.escape(json.dumps(result, ensure_ascii=False))}\n' > f'</details>\n' > ) > yield {"choices": [{"delta": {"content": details_block}, "finish_reason": None}]} > > yield {"choices": [{"delta": {"content": "Done."}, "finish_reason": None}]} > yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} > Mapped to your LangChain loop: > > import html, json > > for chunk in agent.stream({"messages": messages}, stream_mode=["updates", "messages"]): > if chunk["type"] == "messages": > message = chunk["data"][0] > > if isinstance(message, AIMessageChunk): > # Stream content only — drop message.tool_calls entirely. > if message.content: > yield {"choices": [{"delta": {"content": message.content}, "finish_reason": None}]} > > elif isinstance(message, ToolMessage): > details = ( > f'<details type="tool_calls" done="true" ' > f'id="{message.tool_call_id}" name="{message.name}" ' > f'arguments="{html.escape(json.dumps(getattr(message, "args", {}) or {}))}">\n' > f'<summary>Tool Executed</summary>\n' > f'{html.escape(json.dumps(message.content, ensure_ascii=False, default=str))}\n' > f'</details>\n' > ) > yield {"choices": [{"delta": {"content": details}, "finish_reason": None}]} > > # final chunk > yield {"choices": [{"delta": {}, "finish_reason": "stop"}]} > Rule of thumb: **`delta.tool_calls` always means "please execute this for me, client."** If your agent has already executed the tool, use content-embedded `<details>` blocks instead. That's exactly what Open WebUI's own server-side tool path emits after execution — you're producing the same output shape directly, skipping the execute-and-continue loop that doesn't apply when the agent owns tool execution. Thank you! I dont know if this is a bug but ```yield {"choices": [{"delta": {"content": details}, "finish_reason": None}]}``` did not seem to work but ```yield details``` did
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#58792