Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai python Shared Function Definition

From Leeroopedia
Revision as of 13:41, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Openai_Openai_python_Shared_Function_Definition.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains API_Types, Python
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for defining a function that the model can call provided by the openai-python SDK.

Description

FunctionDefinition is a Pydantic BaseModel that describes a callable function for the OpenAI function calling feature. It requires a name field (alphanumeric with underscores and dashes, max 64 characters). Optional fields include description (used by the model to determine when and how to call the function), parameters (a FunctionParameters JSON Schema object describing accepted arguments), and strict (a boolean that, when True, enforces strict schema adherence using Structured Outputs).

Usage

Import FunctionDefinition when constructing tool definitions for chat completions or responses API calls that use function calling.

Code Reference

Source Location

Signature

class FunctionDefinition(BaseModel):
    name: str
    description: Optional[str] = None
    parameters: Optional[FunctionParameters] = None
    strict: Optional[bool] = None

Import

from openai.types.shared import FunctionDefinition

I/O Contract

Fields

Name Type Required Description
name str Yes The name of the function. Must be a-z, A-Z, 0-9, underscores or dashes, max 64 characters.
description Optional[str] No Description of what the function does, used by the model to choose when and how to call it.
parameters Optional[FunctionParameters] No The parameters the function accepts, described as a JSON Schema object. Omitting defines a function with an empty parameter list.
strict Optional[bool] No Whether to enable strict schema adherence. When True, the model follows the exact schema in parameters. Only a subset of JSON Schema is supported in strict mode.

Usage Examples

from openai import OpenAI
from openai.types.shared import FunctionDefinition

client = OpenAI()

# Define a function for the model to call
weather_function = FunctionDefinition(
    name="get_weather",
    description="Get the current weather for a given location",
    parameters={
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "City and state, e.g. San Francisco, CA",
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"],
            },
        },
        "required": ["location"],
    },
    strict=True,
)

# Use in a chat completion with tools
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=[{"type": "function", "function": weather_function.model_dump()}],
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment