Generate text and then call tool

I’m trying to build a flow where the LLM can decide to call a tool after it generates the answer. GPT-4o seems to be able to it (gpt-4-turbo can’t). For example if I ask GPT-4o to tell me a joke and them give tell what temperature is, it indeed tells a joke and then calls the tool get_weather (using the traditional get weather example).

But for more complex prompt, I’m getting this behaviour: it generates the corrrect the text but ends it with:

     **Função a ser executada:**

       "code": "1602"
    ### `get_heading_description`
    Headings: "1602".


Also, the response do contains the tool call get_heading_description

What do you think? Is this the expected behaviour? Behind the scenes, how are the tools being called? Like this and then OpenAI process the text generated text, searching for these re patterns? If so, why the process did found them an is showing in the final text generation?

Providing a full example, including the call to the get_heading_description tool, would help us better understand the circumstances under which this occurs.

I know but I can’t. It contains proprietary data.

But the instructions are equivalent: first think and explain and then instructed to call get_heading_description in case it thinks it will be helpfull to its conclusion. And it works, sort of. It generates the text and at the end calls the function. But this extra text is annoying.