Python function signatures to OpenAI-compatible JSON Schema
Project description
matrix-fn-schema
Convert Python function signatures (type annotations + docstrings) into OpenAI-compatible JSON Schema (tool call format).
from dataclasses import dataclass
from enum import Enum
from typing import Annotated, List, Optional, Literal
from uuid import UUID
from matrix_fn_schema import build_json_schema
class Priority(int, Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
@dataclass
class Attachment:
filename: str
url: str
def create_task(
title: Annotated[str, "Max 200 chars"],
priority: Priority,
assignee_id: UUID,
attachments: List[Attachment],
deadline: Optional[str] = None,
) -> dict:
"""Create a new task."""
...
schema = build_json_schema(create_task)
Result:
{
"type": "function",
"name": "create_task",
"description": "Create a new task.",
"strict": true,
"parameters": {
"type": "object",
"properties": {
"title": {"type": "string"},
"priority": {"type": "integer", "enum": [1, 2, 3]},
"assignee_id": {"type": "string", "format": "uuid"},
"attachments": {"type": "array", "items": {"type": "object", "properties": {
"filename": {"type": "string"},
"url": {"type": "string"}
}, "required": ["filename", "url"], "additionalProperties": false}},
"deadline": {"anyOf": [{"type": "string"}, {"type": "null"}]}
},
"additionalProperties": false,
"required": ["title", "priority", "assignee_id", "attachments", "deadline"]
}
}
Supported types
| Python | JSON Schema |
|---|---|
int |
{"type": "integer"} |
float |
{"type": "number"} |
str |
{"type": "string"} |
bool |
{"type": "boolean"} |
None |
{"type": "null"} |
Literal["a", "b"] |
{"enum": ["a", "b"]} |
list[X] |
{"type": "array", "items": <X>} |
tuple[X, Y] |
{"type": "array", "prefixItems": [...], "minItems": N, "maxItems": N} |
tuple[X, ...] |
{"type": "array", "items": <X>} |
dict[K, V] |
{"type": "object", "additionalProperties": <V>} |
Optional[X] / X | None |
{"anyOf": [<X>, {"type": "null"}]} |
Union[X, Y, Z] / X | Y | Z |
{"anyOf": [<X>, <Y>, <Z>]} |
Annotated[T, ...] |
unwrapped to T |
Final[T] |
unwrapped to T |
enum.Enum (str values) |
{"type": "string", "enum": [...]} |
enum.IntEnum |
{"type": "integer", "enum": [...]} |
uuid.UUID |
{"type": "string", "format": "uuid"} |
datetime.datetime |
{"type": "string", "format": "date-time"} |
datetime.date |
{"type": "string", "format": "date"} |
datetime.time |
{"type": "string", "format": "time"} |
@dataclass |
recursive {"type": "object", "properties": {...}} |
TypedDict |
recursive {"type": "object"} with Required/NotRequired |
Self / recursive types |
cycle-safe (breaks at the self-reference) |
pydantic.BaseModel |
delegated to .model_json_schema() (optional dep) |
Requirements
Python 3.10+. Dependencies: docstring-parser>=0.16.
Optional: pydantic>=2 for BaseModel support.
Written with love by dotmatrix.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matrix_fn_schema-0.1.4.tar.gz.
File metadata
- Download URL: matrix_fn_schema-0.1.4.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70c078e06489c110ee2bdc89d1f2b9ee38751d7323d2699db94b962fa1c2c992
|
|
| MD5 |
127da2a8321803531907390e248fa892
|
|
| BLAKE2b-256 |
cbd85dba954cc728e6d4290157284834f374d084bcdeb1292901e6a97c25fb9e
|
File details
Details for the file matrix_fn_schema-0.1.4-py3-none-any.whl.
File metadata
- Download URL: matrix_fn_schema-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
089a385b1035e694be30f63f0aecb76e356231fed0362a19f11baba67a54ad8d
|
|
| MD5 |
dec4d6e6910426e3310a4795a3ef3be1
|
|
| BLAKE2b-256 |
e665fc21cc1d8cd3ad8d359c4945f50493bd13e3279e60634c5a9b5870b69c60
|