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.7

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.7
File Size Uploaded
matrix_fn_schema-0.1.7.tar.gz 6.2 kB Details

Built distribution (wheel)

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

Total release size: 12.8 kB

Release files / matrix_fn_schema-0.1.7.tar.gz

Download URL matrix_fn_schema-0.1.7.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f8e542911711f3fe86049521f519f20932a15277a716b73b0214f78e16e6b916
BLAKE2b-256 checksum
How to use checksums
e0baa874000d08169a5891254c617ac7ed6dfd90ba2081a6c73606941032fba8
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.7-py3-none-any.whl

Download URL matrix_fn_schema-0.1.7-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a521d4ca5fee6ce96b3d160906b8d5cf129e17b3facd8cfb3cb81b6106f581ea
BLAKE2b-256 checksum
How to use checksums
77bec07a30afc86a67e2554a49938136712b4b05ce930be6c13e2effed1d3da9
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

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

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