Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/smolagents/prompts/code_agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ system_prompt: |-

At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
Then in the Code sequence you should write the code in simple Python. The code sequence must be opened with '{{code_block_opening_tag}}', and closed with '{{code_block_closing_tag}}'.
STRICT FORMAT RULES (enforced globally):
- Always output exactly one 'Thought:' line followed by exactly one code block.
- The code block MUST be wrapped using '{{code_block_opening_tag}}' and '{{code_block_closing_tag}}'.
- Never use triple backticks anywhere in your response.
- Do not output any prose or extra text after '{{code_block_closing_tag}}'.
- Ensure the code block is present every step so the executor can parse it.
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
In the end you have to return a final answer using the `final_answer` tool.
Expand Down Expand Up @@ -165,7 +171,9 @@ system_prompt: |-
8. Never create any notional variables in our code, as having these in your logs will derail you from the true variables.
9. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
10. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
11. Don't give up! You're in charge of solving the task, not providing directions to solve it.
11. Never use triple backticks. Only the configured '{{code_block_opening_tag}}'/'{{code_block_closing_tag}}' are allowed for code.
12. Do not output any content after '{{code_block_closing_tag}}'.
13. Don't give up! You're in charge of solving the task, not providing directions to solve it.

{%- if custom_instructions %}
{{custom_instructions}}
Expand Down
13 changes: 11 additions & 2 deletions src/smolagents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,20 @@ def parse_json_blob(json_blob: str) -> tuple[dict[str, str], str]:


def extract_code_from_text(text: str, code_block_tags: tuple[str, str]) -> str | None:
"""Extract code from the LLM's output."""
"""Extract code from the LLM's output.

Also strips any occurrences of triple backticks ``` that may appear
inside the extracted code blocks.
"""
pattern = rf"{code_block_tags[0]}(.*?){code_block_tags[1]}"
matches = re.findall(pattern, text, re.DOTALL)
if matches:
return "\n\n".join(match.strip() for match in matches)
cleaned_matches = []
for match in matches:
# Remove any triple backticks found inside the code content to fix the persistent code block parsing error
cleaned = match.replace("```", "").strip()
cleaned_matches.append(cleaned)
return "\n\n".join(cleaned_matches)
return None


Expand Down