Course contentsShow
← Machine Learning and Deep Learning
Lesson 1926 of 3,53841. Structured Output and Tool CallingFree lesson

Executing Functions and Returning Results

Running the called function with extracted arguments and formatting results for the next LLM turn.

Executing Functions and Returning Results

What you'll learn: How to actually run the function an LLM requested and feed the output back into the conversation for the model to continue reasoning.

The Execution Bridge

When an LLM generates a function call (lesson 1925), it's only describing what should happen—it's not actually executing code. Think of it like a customer ordering food: they tell you what they want, but someone still needs to prepare and deliver it.

Your application code must:

  1. Extract the function name and arguments from the LLM's response
  2. Execute the actual function in your environment
  3. Format the result as a new message to send back to the LLM

The Round-Trip Pattern

# Step 1: LLM requests function call
# {"name": "get_weather", "arguments": {"city": "Paris"}}

# Step 2: Your code executes the function
result = get_weather(city="Paris")  # Returns: {"temp": 18, "condition": "cloudy"}

# Step 3: Format result as a "function" role message
messages.append({
    "role": "function",
    "name": "get_weather",
    "content": json.dumps(result)
})

# Step 4: Send back to LLM for final answer
final_response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=messages
)

Result Formatting Matters

The function result should be:

  • JSON-serialized (even if it's just a string or number)
  • Concise but complete (avoid dumping massive payloads)
  • Error-aware (if the function fails, return a structured error message)

The LLM uses this result to generate its final user-facing response, like "The weather in Paris is currently 18°C and cloudy."

Key Takeaway: Function calling is a three-step dance: LLM requests → you execute → LLM synthesizes the result into a natural language answer.