Course contentsShow
← AI Engineering
Lesson 553 of 1,88614. Function Calling and Tool UseFree lesson

Function Calling Response Formats

Parsing function_call and tool_calls objects from API responses and extracting arguments safely.

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, function object
  • The nested function contains name and arguments

Safe Argument Extraction

The arguments field is always a JSON string, not a parsed object. You must:

  1. Check if the response contains function/tool calls
  2. Extract the arguments string
  3. Parse it with json.loads() wrapped in error handling
  4. 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.