Skip to main content

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.

Metadata

Release files for matrix-fn-schema 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for matrix-fn-schema 0.1.5
File Size Uploaded
matrix_fn_schema-0.1.5.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for matrix-fn-schema 0.1.5
File Interpreter ABI Platform
matrix_fn_schema-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / matrix_fn_schema-0.1.5.tar.gz

Download URL matrix_fn_schema-0.1.5.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4ed219c1096ed663761c92a956d6b450f038e51e486251fc17c7f33374cec818
BLAKE2b-256 checksum
How to use checksums
862671682fd8dca8d7efbf7507790c14e344ec2fea98492a39328076ada418b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","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}

Release files / matrix_fn_schema-0.1.5-py3-none-any.whl

Download URL matrix_fn_schema-0.1.5-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
155d7e0b4ce5bdcc8983423f4e674eeac5ffe67e76173f5091a7999a05a35e8e
BLAKE2b-256 checksum
How to use checksums
cda57e396917a0d70479685c581ae5f767f9711408e894f7a3566b6bcf74422b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","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}

Release history Release notifications | RSS feed

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page