GenAI Lesson 42 – Function Calling | Dataplexa

Function Calling: Connecting Language Models to Real Actions

Large Language Models are excellent at generating text.

However, real-world applications require models to interact with systems, databases, APIs, and business logic.

Function calling enables this connection between natural language and executable code.

The Core Limitation of Text-Only Models

Without function calling, models can:

  • Describe what should happen
  • Explain how to do something
  • Simulate outcomes in text

But they cannot actually perform actions.

This creates a gap between intelligence and execution.

What Function Calling Solves

Function calling allows a model to:

  • Select the correct function
  • Extract structured arguments
  • Trigger real code execution

The model becomes a decision-maker, not an executor.

How Function Calling Works Conceptually

The model is shown:

  • A list of available functions
  • Each function’s name
  • Its expected parameters and schema

Based on the user’s request, the model decides whether to call a function.

Thinking Like a System Designer

Before implementing function calling, engineers decide:

  • Which actions should be automated
  • What inputs must be validated
  • What the model should never execute

The model should not have unrestricted control.

Defining Functions for the Model

Functions are defined using a structured schema.


functions = [
  {
    "name": "get_weather",
    "description": "Get current weather for a city",
    "parameters": {
      "type": "object",
      "properties": {
        "city": {"type": "string"}
      },
      "required": ["city"]
    }
  }
]
  

This schema teaches the model what tools are available.

What the Model Actually Produces

When the model decides to use a function, it does not execute code.

It returns structured output describing the call.


{
  "name": "get_weather",
  "arguments": {
    "city": "San Francisco"
  }
}
  

This output is parsed by your application.

Where Code Execution Happens

Execution happens outside the model.

Your system:

  • Receives the function call
  • Validates parameters
  • Runs the function safely

The model never touches your infrastructure directly.

Calling the Function in Application Code

This is how developers connect model output to real logic.


result = get_weather(city="San Francisco")
  

The result is then sent back to the model as context.

Why This Two-Step Design Is Important

Separating decision and execution:

  • Improves security
  • Prevents hallucinated execution
  • Allows auditing and logging

This is critical for production systems.

Real-World Use Cases

  • Booking systems
  • Customer support automation
  • Data retrieval assistants
  • Workflow orchestration

Function calling enables AI-driven automation.

Common Mistakes to Avoid

  • Allowing unrestricted function access
  • Skipping input validation
  • Trusting model output blindly

Function calling must be treated like an API layer.

How Learners Should Practice This

Effective practice includes:

  • Designing simple function schemas
  • Testing ambiguous user inputs
  • Simulating safe execution paths

The focus is system design, not just prompt writing.

Practice

What does the model return during function calling?



Where does actual code execution occur?



Why is execution separated from decision-making?



Quick Quiz

What helps the model understand available functions?





Who executes the function?





Function calling primarily enables?





Recap: Function calling allows LLMs to trigger real-world actions safely through structured interfaces.

Next up: Multimodal Models — combining text, images, audio, and more.