Repository: ErikBjare/gptme
File: gptme/llm/llm_openai.py (Python)

Code before:
                # For a message part of type `text`` we try to extract the tool_uses
                # We search line by line to stop as soon as we have a tool call
                # It makes it easier to split in multiple parts.
                for line in message_part["text"].split("\n"):
                    text += line + "\n"

                    tooluses = [
                        tooluse
                        for tooluse in ToolUse.iter_from_content(text)
                        if tooluse.is_runnable
                    ]
                    if not tooluses:
                        continue

                    # At that point we should always have exactly one tooluse
                    # Because we remove the previous ones as soon as we encounter
                    # them so we can't have more.
                    assert len(tooluses) == 1
                    tooluse = tooluses[0]
                    before_tool = text[: tooluse.start]

                    if before_tool.replace("\n", ""):
                        content.append({"type": "text", "text": before_tool})

                    tool_calls.append(
                        {
                            "id": tooluse.call_id or "",
                            "type": "function",
                            "function": {
                                "name": tooluse.tool,
                                "arguments": json.dumps(tooluse.kwargs or {}),
                            },
                        }
                    )
                    # The text is emptied to start over with the next lines if any.
                    text = ""

            if content:
                modified_message["content"] = content

            if tool_calls:
                # Clean content property if empty otherwise the call fails
                if not content:
                    del modified_message["content"]
                modified_message["tool_calls"] = tool_calls

            yield modified_message
        else:
            yield message



Review comment (suggestion):
```suggestion
                        if before_tool.strip():
```

Code after:
                # For a message part of type `text`` we try to extract the tool_uses
                # We search line by line to stop as soon as we have a tool call
                # It makes it easier to split in multiple parts.
                for line in message_part["text"].split("\n"):
                    text += line + "\n"

                    tooluses = [
                        tooluse
                        for tooluse in ToolUse.iter_from_content(text)
                        if tooluse.is_runnable
                    ]
                    if not tooluses:
                        continue

                    # At that point we should always have exactly one tooluse
                    # Because we remove the previous ones as soon as we encounter
                    # them so we can't have more.
                    assert len(tooluses) == 1
                    tooluse = tooluses[0]

                    # We only want to add a tool call if we have a call_id which
                    # means it is a tool response
                    if tooluse.call_id:
                        before_tool = text[: tooluse.start]

                        if before_tool.replace("\n", ""):
                            content.append({"type": "text", "text": before_tool})

                        tool_calls.append(
                            {
                                "id": tooluse.call_id or "",
                                "type": "function",
                                "function": {
                                    "name": tooluse.tool,
                                    "arguments": json.dumps(tooluse.kwargs or {}),
                                },
                            }
                        )
                    else:
                        content.append({"type": "text", "text": text})
                    # The text is emptied to start over with the next lines if any.
                    text = ""

            if content:
                modified_message["content"] = content

            if tool_calls:
                # Clean content property if empty otherwise the call fails
                if not content:
                    del modified_message["content"]
                modified_message["tool_calls"] = tool_calls