Function Calling Response Formats
What you'll learn: How to safely parse and extract function arguments from LLM API responses when using function calling.
Understanding the Response Structure
When an LLM decides to call a function, the API doesn't execute anything—it returns a specially formatted response telling you which function to call and with what arguments. Your code must parse this response correctly to extract the function name and arguments safely.
Think of it like receiving a formal request form: the LLM fills out which service (function) it wants and the details (arguments), but you're responsible for reading the form correctly and taking action.
Two Response Formats
Depending on the provider and API version, you'll encounter:
function_call object (older OpenAI format):
- Contains
name(string) — the function to call - Contains
arguments(JSON string) — the parameters
tool_calls array (newer format supporting parallel calls):
- Each item has
id,type,functionobject - The nested
functioncontainsnameandarguments
Safe Argument Extraction
The arguments field is always a JSON string, not a parsed object. You must:
- Check if the response contains function/tool calls
- Extract the
argumentsstring - Parse it with
json.loads()wrapped in error handling - Validate the parsed data matches your expected schema
Never assume the JSON is valid or complete—LLMs can generate malformed JSON. Always use try/except blocks when parsing.
Key Takeaway: Function calling responses require careful parsing—always treat arguments as an untrusted JSON string that needs validation before executing any function.