The problem with regex

Earlier, we parsed tool calls from the LLM text using regex. It worked for simple cases, but this approach is fragile. Let us see why.

03-tool-integration.ipynb
python
# These all break our regex parser:

# Extra whitespace
"Action:  calculate"      # Double space, regex fails

# Different casing
"action: calculate"       # Lowercase, regex fails

# Slightly different format
"Tool: calculate"         # Wrong label, regex fails

# Multiple actions in one response
"Action: get_weather\nAction: calculate"  # Only first is caught

Small format variations cause regex parsing to fail silently, and the agent breaks without warning.

Text parsing vs structured function calling

Quiz: Quiz

Loading practice…

The solution: modern LLMs are fine-tuned to return structured tool calls as part of their API response. Instead of parsing text, you get a clean JSON object with the function name and arguments. This is called native function calling.