Skip to main content
The function schema functionality in GraphAI provides a powerful way to generate standardized function schemas that can be used with various LLM providers. This feature allows you to automatically generate function schemas from your Python functions, making it easier to integrate with LLM function calling capabilities.

Overview

The FunctionSchema class is designed to consume Python functions and generate schemas that are compatible with different LLM providers (OpenAI, Ollama, LiteLLM, etc.). It automatically extracts:
  • Function name
  • Function description (from docstring)
  • Function signature
  • Return type
  • Parameters (including types, defaults, and required status)

Basic Usage

Here’s a simple example of how to use the function schema functionality:

Schema Structure

The generated schema follows a standardized format:

Parameter Types

The schema automatically maps Python types to LLM-compatible types:
  • intnumber
  • floatnumber
  • strstring
  • boolboolean
  • Other types → object

Working with Multiple Functions

You can generate schemas for multiple functions at once using the get_schemas utility:

Pydantic Model Support

The function schema functionality also supports generating schemas from Pydantic models:

Best Practices

  1. Documentation: Always include docstrings for your functions. The schema generator will use these as descriptions.
  2. Type Hints: Use type hints for all parameters and return values to ensure proper type mapping.
  3. Default Values: Consider using default values for optional parameters.
  4. Required Parameters: Parameters without default values are automatically marked as required.

Integration with LLM Providers

The generated schemas are compatible with major LLM interfaces such as OpenAI, LiteLLM, Ollama, and others. Most providers use the same schema format which can be generated with to_dict.