Create OpenAI-compatible function schemas from python functions
Project description
Py2OpenAI
OpenAI Function Schema Generator
Convert Python functions to OpenAI-compatible function schemas automatically.
Installation
pip install openai-function-schema # not yet published
Basic Usage
from openai_function_schema import create_schema
from typing import Literal
def get_weather(
location: str,
unit: Literal["C", "F"] = "C",
detailed: bool = False,
) -> dict[str, str | float]:
"""Get the weather for a location.
Args:
location: City or address to get weather for
unit: Temperature unit (Celsius or Fahrenheit)
detailed: Include extended forecast
"""
return {"temp": 22.5, "conditions": "sunny"}
# Create schema
schema = create_schema(get_weather)
# Use with OpenAI
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in London?"}],
functions=[schema.model_dump_openai()],
function_call="auto"
)
Supported Types
Basic Types
def func(
text: str, # -> "type": "string"
number: int, # -> "type": "integer"
amount: float, # -> "type": "number"
enabled: bool, # -> "type": "boolean"
anything: Any, # -> "type": "string"
) -> None: ...
Container Types
def func(
items: list[str], # -> "type": "array", "items": {"type": "string"}
numbers: set[int], # -> same as list
mapping: dict[str, Any], # -> "type": "object", "additionalProperties": true
nested: list[dict[str, int]], # -> nested array/object types
sequence: Sequence[str], # -> "type": "array"
collection: Collection[int], # -> "type": "array"
) -> None: ...
Enums and Literals
class Color(Enum):
RED = "red"
BLUE = "blue"
def func(
color: Color, # -> "type": "string", "enum": ["red", "blue"]
mode: Literal["fast", "slow"], # -> "type": "string", "enum": ["fast", "slow"]
) -> None: ...
Optional and Union Types
def func(
opt1: str | None, # -> "type": "string"
opt2: int | None, # -> "type": "integer"
union: str | int, # -> "type": "string" (first type)
) -> None: ...
Custom Types
@dataclass
class User:
name: str
age: int
def func(
user: User, # -> "type": "object"
data: JsonDict, # -> "type": "object"
) -> None: ...
Type Aliases
JsonValue = dict[str, Any] | list[Any] | str | int | float | bool | None
JsonDict = dict[str, JsonValue]
def func(
data: JsonDict, # -> "type": "object"
values: list[JsonValue], # -> "type": "array"
) -> None: ...
Recursive Types
def func(
tree: dict[str, "dict[str, Any] | str"], # -> "type": "object"
nested: dict[str, list["dict[str, Any]"]], # -> "type": "object"
) -> None: ...
Generated Schema Example
{
"name": "get_weather",
"description": "Get the weather for a location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City or address to get weather for"
},
"unit": {
"type": "string",
"enum": ["C", "F"],
"description": "Temperature unit (Celsius or Fahrenheit)",
"default": "C"
},
"detailed": {
"type": "boolean",
"description": "Include extended forecast",
"default": false
}
},
"required": ["location"]
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py2openai-0.0.1.tar.gz
(17.8 kB
view details)
Built Distribution
File details
Details for the file py2openai-0.0.1.tar.gz
.
File metadata
- Download URL: py2openai-0.0.1.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49e1d692eab1e96daad9cdf3da4a9219fc3e32efe1219d3e0b847e2bc3f2db2e |
|
MD5 | 976d23e4c762a7a79c631e6211efae7d |
|
BLAKE2b-256 | d6096cd26a9d033d97e3e368637cabc8a5c4e9869f31bd8823b63658d6c877b9 |
File details
Details for the file py2openai-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: py2openai-0.0.1-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d17b4e018c5bab5320334716a88a31f93c540a5f11f30f0ffec53e345f858230 |
|
MD5 | e23bb5dea98323350734df173a19da89 |
|
BLAKE2b-256 | 5fe2756fe6218dcf44104f74772d836e31a8c49784f61b594a1269af689db2d7 |