> ## Documentation Index
> Fetch the complete documentation index at: https://docs.venice.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Function Calling

> Let Venice chat models call your application tools with OpenAI-compatible function calling and the chat completions API.

Function calling lets a model choose structured tool calls that your application can execute. The model does not run the function itself. It returns the function name and arguments, your code runs the function, and you send the result back to the model.

Use function calling when the model needs live data, application actions, database lookups, or deterministic calculations.

## Basic Tool Definition

Define tools with the OpenAI-compatible `tools` array:

<CodeGroup>
  ```python Python theme={"system"}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["VENICE_API_KEY"],
      base_url="https://api.venice.ai/api/v1",
  )

  tools = [
      {
          "type": "function",
          "function": {
              "name": "get_weather",
              "description": "Get the current weather in a location",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {
                          "type": "string",
                          "description": "City and state, such as San Francisco, CA",
                      }
                  },
                  "required": ["location"],
              },
          },
      }
  ]

  response = client.chat.completions.create(
      model="zai-org-glm-5",
      messages=[{"role": "user", "content": "What is the weather in San Francisco?"}],
      tools=tools,
  )

  print(response.choices[0].message.tool_calls)
  ```

  ```javascript Node.js theme={"system"}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.VENICE_API_KEY,
    baseURL: "https://api.venice.ai/api/v1",
  });

  const tools = [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Get the current weather in a location",
        parameters: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "City and state, such as San Francisco, CA",
            },
          },
          required: ["location"],
        },
      },
    },
  ];

  const response = await client.chat.completions.create({
    model: "zai-org-glm-5",
    messages: [{ role: "user", content: "What is the weather in San Francisco?" }],
    tools,
  });

  console.log(response.choices[0].message.tool_calls);
  ```

  ```bash cURL theme={"system"}
  curl https://api.venice.ai/api/v1/chat/completions \
    -H "Authorization: Bearer $VENICE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "zai-org-glm-5",
      "messages": [
        {"role": "user", "content": "What is the weather in San Francisco?"}
      ],
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_weather",
            "description": "Get the current weather in a location",
            "parameters": {
              "type": "object",
              "properties": {
                "location": {
                  "type": "string",
                  "description": "City and state, such as San Francisco, CA"
                }
              },
              "required": ["location"]
            }
          }
        }
      ]
    }'
  ```
</CodeGroup>

## Execute the Tool

When the model chooses a tool, inspect `message.tool_calls`, parse the arguments, execute your application function, then send the result back as a `tool` message.

```python Python theme={"system"}
import json

message = response.choices[0].message
tool_call = message.tool_calls[0]
arguments = json.loads(tool_call.function.arguments)

weather = get_weather(arguments["location"])

follow_up = client.chat.completions.create(
    model="zai-org-glm-5",
    messages=[
        {"role": "user", "content": "What is the weather in San Francisco?"},
        message.model_dump(),
        {
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(weather),
        },
    ],
    tools=tools,
)

print(follow_up.choices[0].message.content)
```

## Choose a Model

Function calling support is model-specific. Use the [Text Models](/models/text) page or the [Models API](/api-reference/endpoint/models/list) to find models with `supportsFunctionCalling`.

<Warning>
  Treat tool arguments as untrusted input. Validate arguments before using them in database queries, shell commands, payments, or other side-effecting operations.
</Warning>

## Design Tips

* Keep tool names and descriptions short and literal.
* Use JSON Schema to make valid arguments easy for the model to produce.
* Prefer narrow tools with clear inputs over one broad tool with many optional behaviors.
* Return concise tool results so the final answer has enough context without wasting tokens.

## Related Resources

* [Chat Completions API](/api-reference/endpoint/chat/completions)
* [Text Models](/models/text)
* [Structured Responses Guide](/guides/features/structured-responses)
* [LangChain Integration](/guides/integrations/langchain#function-calling-with-agents)
