Skip to content

Exclude thinking generation from python parsing #1404

@sbucaille

Description

@sbucaille

Hey,

Is your feature request related to a problem? Please describe.
Sometimes, my LLM steps ends with final_answer(...) not being included in the required ``py `` code block syntax, so smolagents raises an error and starts a new step telling the LLM that the syntax was wrong.
The problem comes from the fact that I was using a reasoning LLM (qwen 14b in my case), which first reflects on the problem by generating its reasoning between <think> </think> tags, at some point, it plans to use the required syntax by generating it in its thoughts, but without code in it.

Step 1 not including the python block syntax :

<think>                                                                                                                                                                                                           
Okay, the user wanted to add a component with the text "hello world" to their Gradio interface. I used the interface_agent tool to handle that. The agent was instructed to add a text component with the label   
"hello world". The response from the agent said the task was completed. Now I need to confirm that the component was added successfully.                                                                          
                                                                                                                                                                                                                  
I should check if there's any feedback or confirmation needed. Since the user didn't specify further actions, maybe they just want to know it's done. The final answer should be a simple confirmation. Let me    
make sure the response is clear and concise.                                                                                                                                                                      
</think>                                                                                                                                                                                                          
                                                                                                                                                                                                                  
The text component with the label "hello world" has been successfully added to the Gradio interface.                                                                                                              
                                                                                                                                                                                                                  
final_answer("The 'hello world' text component has been added to the Gradio interface.")

Step 2 thinking about adding it but stopped because the python code block syntax got detected by the parser :

<think>                                                                                                                                                                                                           
Okay, the user wants me to add a component with the text "hello world" to their Gradio interface. Let me think about how to do this correctly.                                                                    
                                                                                                                                                                                                                  
First, I remember that the interface_agent tool is used for modifying the Gradio interface. So I should use that. The task needs to be clearly described. The user mentioned adding a component with the text     
"hello world", so the label should be "hello world".                                                                                                                                                              
                                                                                                                                                                                                                  
Wait, in the previous attempt, I called the interface_agent with the correct instruction, and the agent responded that the task was completed. But the user is now showing an error about code parsing. Maybe the 
way I structured the code was incorrect. The user's system might be expecting a different format for the code snippet.                                                                                            
                                                                                                                                                                                                                  
Let me check the rules again. Oh right, the code must be in a specific format with ```py and end with                                                                                                             
Error in code parsing:
Your code snippet is invalid, because the regex pattern ```(?:py|python)?\s*\n(.*?)\n``` was not found in it.

Here is the full example for more clarity : https://app.warp.dev/block/embed/hUU6K5aEKJKoJZJMpKuPcR

Describe the solution you'd like

Prevent parsing code generation if a <think> tag appeared without a closing </think> tag
Adding a new rule in fix_final_answer_code may solve the problem.
I am willing to contribute if you want.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions