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

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.9
File Size Uploaded
matrix_fn_schema-0.1.9.tar.gz 6.4 kB Details

Built distribution (wheel)

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

Total release size: 13.1 kB

Release files / matrix_fn_schema-0.1.9.tar.gz

Download URL matrix_fn_schema-0.1.9.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fd6246259ce59cfdae3b15050cde0b35ecf87555b37d92de9d15e973f17cd166
BLAKE2b-256 checksum
How to use checksums
1a44720fe1a0653d9d9825f48ed53c149193016ca1536d683435bf49ce036011
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","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.9-py3-none-any.whl

Download URL matrix_fn_schema-0.1.9-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0dc7181c17e574741cddfc0cd9f40691532dfac89ed142d8b877d4ae5ce80353
BLAKE2b-256 checksum
How to use checksums
56278c85c641a0c412ed1c6ebf65e18252f31061fff52b6e122a4120e13b3080
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","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

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

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