schemez 1.2.2__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of schemez might be problematic. Click here for more details.
- schemez-2.0.1/PKG-INFO +404 -0
- schemez-2.0.1/README.md +360 -0
- {schemez-1.2.2 → schemez-2.0.1}/pyproject.toml +1 -1
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/__init__.py +4 -0
- schemez-2.0.1/src/schemez/code_generation/__init__.py +6 -0
- schemez-2.0.1/src/schemez/code_generation/namespace_callable.py +79 -0
- schemez-2.0.1/src/schemez/code_generation/route_helpers.py +126 -0
- schemez-2.0.1/src/schemez/code_generation/tool_code_generator.py +162 -0
- schemez-2.0.1/src/schemez/code_generation/toolset_code_generator.py +444 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/executable.py +11 -9
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/functionschema.py +428 -104
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/helpers.py +50 -95
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/schema.py +132 -83
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/tool_executor/executor.py +7 -6
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/tool_executor/helpers.py +3 -3
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/typedefs.py +2 -6
- schemez-1.2.2/PKG-INFO +0 -340
- schemez-1.2.2/README.md +0 -296
- {schemez-1.2.2 → schemez-2.0.1}/LICENSE +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/bind_kwargs.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/code.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/convert.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/create_type.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/docstrings.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/log.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/py.typed +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/pydantic_types.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/schema_generators.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/schemadef/__init__.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/schemadef/schemadef.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/tool_executor/__init__.py +0 -0
- {schemez-1.2.2 → schemez-2.0.1}/src/schemez/tool_executor/types.py +0 -0
schemez-2.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: schemez
|
|
3
|
+
Version: 2.0.1
|
|
4
|
+
Summary: Pydantic shim for config stuff
|
|
5
|
+
Keywords:
|
|
6
|
+
Author: Philipp Temminghoff
|
|
7
|
+
Author-email: Philipp Temminghoff <philipptemminghoff@googlemail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: Pydantic
|
|
12
|
+
Classifier: Framework :: Pydantic :: 2
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Documentation
|
|
21
|
+
Classifier: Topic :: Software Development
|
|
22
|
+
Classifier: Topic :: Utilities
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Dist: docstring-parser>=0.17.0
|
|
25
|
+
Requires-Dist: griffe>=1.7.3
|
|
26
|
+
Requires-Dist: pydantic
|
|
27
|
+
Requires-Dist: universal-pathlib>=0.2.6
|
|
28
|
+
Requires-Dist: llmling-agent ; extra == 'ai'
|
|
29
|
+
Requires-Dist: anyenv>=0.4.14 ; extra == 'ai'
|
|
30
|
+
Requires-Dist: datamodel-code-generator ; extra == 'codegen'
|
|
31
|
+
Requires-Dist: fastapi>=0.118.2 ; extra == 'tool-execution'
|
|
32
|
+
Requires-Dist: yamling ; extra == 'yaml'
|
|
33
|
+
Requires-Python: >=3.13
|
|
34
|
+
Project-URL: Code coverage, https://app.codecov.io/gh/phil65/schemez
|
|
35
|
+
Project-URL: Discussions, https://github.com/phil65/schemez/discussions
|
|
36
|
+
Project-URL: Documentation, https://phil65.github.io/schemez/
|
|
37
|
+
Project-URL: Issues, https://github.com/phil65/schemez/issues
|
|
38
|
+
Project-URL: Source, https://github.com/phil65/schemez
|
|
39
|
+
Provides-Extra: ai
|
|
40
|
+
Provides-Extra: codegen
|
|
41
|
+
Provides-Extra: tool-execution
|
|
42
|
+
Provides-Extra: yaml
|
|
43
|
+
Description-Content-Type: text/markdown
|
|
44
|
+
|
|
45
|
+
# Schemez
|
|
46
|
+
|
|
47
|
+
[](https://pypi.org/project/schemez/)
|
|
48
|
+
[](https://pypi.org/project/schemez/)
|
|
49
|
+
[](https://pypi.org/project/schemez/)
|
|
50
|
+
[](https://pypi.org/project/schemez/)
|
|
51
|
+
[](https://pypi.org/project/schemez/)
|
|
52
|
+
[](https://pypi.org/project/schemez/)
|
|
53
|
+
[](https://pypi.org/project/schemez/)
|
|
54
|
+
[](https://github.com/phil65/schemez/releases)
|
|
55
|
+
[](https://github.com/phil65/schemez/graphs/contributors)
|
|
56
|
+
[](https://github.com/phil65/schemez/discussions)
|
|
57
|
+
[](https://github.com/phil65/schemez/forks)
|
|
58
|
+
[](https://github.com/phil65/schemez/issues)
|
|
59
|
+
[](https://github.com/phil65/schemez/pulls)
|
|
60
|
+
[](https://github.com/phil65/schemez/watchers)
|
|
61
|
+
[](https://github.com/phil65/schemez/stars)
|
|
62
|
+
[](https://github.com/phil65/schemez)
|
|
63
|
+
[](https://github.com/phil65/schemez/commits)
|
|
64
|
+
[](https://github.com/phil65/schemez/releases)
|
|
65
|
+
[](https://github.com/phil65/schemez)
|
|
66
|
+
[](https://github.com/phil65/schemez)
|
|
67
|
+
[](https://codecov.io/gh/phil65/schemez/)
|
|
68
|
+
[](https://pyup.io/repos/github/phil65/schemez/)
|
|
69
|
+
|
|
70
|
+
[Read the documentation!](https://phil65.github.io/schemez/)
|
|
71
|
+
|
|
72
|
+
A powerful toolkit for Python function schema generation and code generation. Extract schemas from functions, generate OpenAI-compatible tools, create HTTP clients, and set up FastAPI routes - all from your function signatures.
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install schemez
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Quick Start
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from schemez import create_schema
|
|
84
|
+
|
|
85
|
+
def get_weather(location: str, unit: str = "C") -> dict:
|
|
86
|
+
"""Get weather for a location."""
|
|
87
|
+
return {"temp": 22, "location": location}
|
|
88
|
+
|
|
89
|
+
# Create schema from function
|
|
90
|
+
schema = create_schema(get_weather)
|
|
91
|
+
print(schema.name) # "get_weather"
|
|
92
|
+
print(schema.description) # "Get weather for a location."
|
|
93
|
+
print(schema.parameters) # Full parameter schema
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## FunctionSchema - The Core
|
|
97
|
+
|
|
98
|
+
The `FunctionSchema` class is the heart of schemez, providing a rich representation of Python functions with powerful methods for introspection and code generation.
|
|
99
|
+
|
|
100
|
+
### Schema Creation
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from schemez import create_schema
|
|
104
|
+
from typing import Literal
|
|
105
|
+
|
|
106
|
+
def search_users(
|
|
107
|
+
query: str,
|
|
108
|
+
limit: int = 10,
|
|
109
|
+
status: Literal["active", "inactive"] = "active",
|
|
110
|
+
include_details: bool = False
|
|
111
|
+
) -> list[dict]:
|
|
112
|
+
"""Search for users with filters.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
query: Search query string
|
|
116
|
+
limit: Maximum number of results
|
|
117
|
+
status: User status filter
|
|
118
|
+
include_details: Include detailed user information
|
|
119
|
+
"""
|
|
120
|
+
return []
|
|
121
|
+
|
|
122
|
+
# Create schema
|
|
123
|
+
schema = create_schema(search_users)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Key Methods
|
|
127
|
+
|
|
128
|
+
#### schema output
|
|
129
|
+
```python
|
|
130
|
+
# Get OpenAI-compatible tool definition
|
|
131
|
+
openai_tool = schema.model_dump_openai()
|
|
132
|
+
# Returns: {"type": "function", "function": {...}}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### Code Generation
|
|
136
|
+
```python
|
|
137
|
+
# Generate Python function signature
|
|
138
|
+
signature = schema.to_python_signature()
|
|
139
|
+
# Returns: "(*, query: str, limit: int = 10, status: Literal['active', 'inactive'] = 'active', include_details: bool = False) -> list[dict]"
|
|
140
|
+
|
|
141
|
+
# Generate Pydantic model for return type
|
|
142
|
+
model_code = schema.to_pydantic_model_code("SearchResponse")
|
|
143
|
+
# Returns Python code string for a Pydantic model
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### Schema Inspection
|
|
147
|
+
```python
|
|
148
|
+
# Access schema components
|
|
149
|
+
print(schema.name) # Function name
|
|
150
|
+
print(schema.description) # Function docstring
|
|
151
|
+
print(schema.parameters) # Parameter schema dict
|
|
152
|
+
print(schema.returns) # Return type schema
|
|
153
|
+
print(schema.get_annotations()) # Python type annotations
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Bulk Schema Generation
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from schemez.schema_generators import (
|
|
160
|
+
create_schemas_from_module,
|
|
161
|
+
create_schemas_from_class,
|
|
162
|
+
create_schemas_from_callables
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# From module
|
|
166
|
+
import math
|
|
167
|
+
schemas = create_schemas_from_module(math, include_functions=['sin', 'cos'])
|
|
168
|
+
|
|
169
|
+
# From class
|
|
170
|
+
class Calculator:
|
|
171
|
+
def add(self, x: int, y: int) -> int:
|
|
172
|
+
"""Add two numbers."""
|
|
173
|
+
return x + y
|
|
174
|
+
|
|
175
|
+
def multiply(self, x: int, y: int) -> int:
|
|
176
|
+
"""Multiply two numbers."""
|
|
177
|
+
return x * y
|
|
178
|
+
|
|
179
|
+
schemas = create_schemas_from_class(Calculator)
|
|
180
|
+
|
|
181
|
+
# From callable list
|
|
182
|
+
functions = [get_weather, search_users]
|
|
183
|
+
schemas = create_schemas_from_callables({"weather": get_weather, "search": search_users})
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Code Generation - Powerful Automation
|
|
187
|
+
|
|
188
|
+
Transform your schemas into executable code for different contexts: HTTP clients, FastAPI routes, and Python execution environments.
|
|
189
|
+
|
|
190
|
+
### HTTP Client Generation
|
|
191
|
+
|
|
192
|
+
Generate complete HTTP client code from schemas:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from schemez.code_generation import ToolsetCodeGenerator
|
|
196
|
+
|
|
197
|
+
# Create from functions
|
|
198
|
+
functions = [get_weather, search_users]
|
|
199
|
+
generator = ToolsetCodeGenerator.from_callables(functions)
|
|
200
|
+
|
|
201
|
+
# Generate HTTP client code
|
|
202
|
+
client_code = generator.generate_client_code(
|
|
203
|
+
base_url="https://api.example.com",
|
|
204
|
+
path_prefix="/v1/tools"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
# Generated code includes:
|
|
208
|
+
# - Pydantic input models for each function
|
|
209
|
+
# - Async HTTP wrapper functions
|
|
210
|
+
# - Type-safe parameter validation
|
|
211
|
+
# - Complete module with imports and exports
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Example generated client:
|
|
215
|
+
```python
|
|
216
|
+
"""Generated HTTP client tools."""
|
|
217
|
+
|
|
218
|
+
from __future__ import annotations
|
|
219
|
+
from pydantic import BaseModel
|
|
220
|
+
import httpx
|
|
221
|
+
|
|
222
|
+
class GetWeatherInput(BaseModel):
|
|
223
|
+
location: str
|
|
224
|
+
unit: str | None = 'C'
|
|
225
|
+
|
|
226
|
+
async def get_weather(input: GetWeatherInput) -> str:
|
|
227
|
+
"""Get weather for a location."""
|
|
228
|
+
async with httpx.AsyncClient() as client:
|
|
229
|
+
response = await client.get(
|
|
230
|
+
"https://api.example.com/v1/tools/get_weather",
|
|
231
|
+
params=input.model_dump(),
|
|
232
|
+
timeout=30.0
|
|
233
|
+
)
|
|
234
|
+
response.raise_for_status()
|
|
235
|
+
return response.text
|
|
236
|
+
|
|
237
|
+
__all__ = ['GetWeatherInput', 'get_weather', ...]
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### FastAPI Route Setup
|
|
241
|
+
|
|
242
|
+
Automatically create FastAPI routes from your functions:
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
from fastapi import FastAPI
|
|
246
|
+
from schemez.code_generation import ToolsetCodeGenerator
|
|
247
|
+
|
|
248
|
+
app = FastAPI()
|
|
249
|
+
|
|
250
|
+
# Create generator with actual callables for execution
|
|
251
|
+
generator = ToolsetCodeGenerator.from_callables([get_weather, search_users])
|
|
252
|
+
|
|
253
|
+
# Add all routes automatically
|
|
254
|
+
generator.add_all_routes(app, path_prefix="/api/tools")
|
|
255
|
+
|
|
256
|
+
# Creates routes:
|
|
257
|
+
# GET /api/tools/get_weather
|
|
258
|
+
# GET /api/tools/search_users
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Python Execution Environment
|
|
262
|
+
|
|
263
|
+
Create sandboxed execution environments with tool functions:
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
# Generate execution namespace
|
|
267
|
+
namespace = generator.generate_execution_namespace()
|
|
268
|
+
|
|
269
|
+
# Execute Python code with tools available
|
|
270
|
+
code = """
|
|
271
|
+
async def main():
|
|
272
|
+
# Tools are available as async functions
|
|
273
|
+
weather = await get_weather(GetWeatherInput(location="London"))
|
|
274
|
+
users = await search_users(SearchUsersInput(query="john", limit=5))
|
|
275
|
+
return {"weather": weather, "users": users}
|
|
276
|
+
"""
|
|
277
|
+
|
|
278
|
+
exec(code, namespace)
|
|
279
|
+
result = await namespace['main']()
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Schema-Only Code Generation
|
|
283
|
+
|
|
284
|
+
Generate code without needing actual function implementations:
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
from schemez import create_schema
|
|
288
|
+
from schemez.code_generation import ToolsetCodeGenerator
|
|
289
|
+
|
|
290
|
+
# Create schemas from function signatures only
|
|
291
|
+
schema1 = create_schema(get_weather)
|
|
292
|
+
schema2 = create_schema(search_users)
|
|
293
|
+
|
|
294
|
+
# Generate client code from schemas
|
|
295
|
+
generator = ToolsetCodeGenerator.from_schemas([schema1, schema2])
|
|
296
|
+
client_code = generator.generate_client_code()
|
|
297
|
+
|
|
298
|
+
# Works for client generation, signatures, models
|
|
299
|
+
# Routes require actual callables for execution
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Tool Documentation
|
|
303
|
+
|
|
304
|
+
Generate comprehensive documentation for your tools:
|
|
305
|
+
|
|
306
|
+
```python
|
|
307
|
+
# Generate tool descriptions with signatures and docstrings
|
|
308
|
+
documentation = generator.generate_tool_description()
|
|
309
|
+
|
|
310
|
+
# Includes:
|
|
311
|
+
# - Function signatures with type hints
|
|
312
|
+
# - Docstrings and parameter descriptions
|
|
313
|
+
# - Usage examples and constraints
|
|
314
|
+
# - Available return type models
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Advanced Features
|
|
318
|
+
|
|
319
|
+
### Type Support
|
|
320
|
+
|
|
321
|
+
Schemez handles complex Python types:
|
|
322
|
+
|
|
323
|
+
```python
|
|
324
|
+
from typing import Literal, Optional, Union
|
|
325
|
+
from enum import Enum
|
|
326
|
+
from dataclasses import dataclass
|
|
327
|
+
|
|
328
|
+
class Status(Enum):
|
|
329
|
+
ACTIVE = "active"
|
|
330
|
+
INACTIVE = "inactive"
|
|
331
|
+
|
|
332
|
+
@dataclass
|
|
333
|
+
class User:
|
|
334
|
+
name: str
|
|
335
|
+
email: str
|
|
336
|
+
|
|
337
|
+
def complex_function(
|
|
338
|
+
users: list[User], # -> Nested object arrays
|
|
339
|
+
status: Status, # -> Enum values
|
|
340
|
+
mode: Literal["fast", "detailed"], # -> String literals
|
|
341
|
+
metadata: dict[str, Any], # -> Objects with any properties
|
|
342
|
+
optional: Optional[str] = None, # -> Optional parameters
|
|
343
|
+
) -> Union[dict, list]: # -> Union return types
|
|
344
|
+
"""Handle complex types."""
|
|
345
|
+
pass
|
|
346
|
+
|
|
347
|
+
schema = create_schema(complex_function)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Configuration
|
|
351
|
+
|
|
352
|
+
Fine-tune schema generation:
|
|
353
|
+
|
|
354
|
+
```python
|
|
355
|
+
# Exclude specific parameter types (e.g., context objects)
|
|
356
|
+
schema = create_schema(my_function, exclude_types=[Context, Session])
|
|
357
|
+
|
|
358
|
+
# Override names and descriptions
|
|
359
|
+
schema = create_schema(
|
|
360
|
+
my_function,
|
|
361
|
+
name_override="custom_name",
|
|
362
|
+
description_override="Custom description"
|
|
363
|
+
)
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Error Handling
|
|
367
|
+
|
|
368
|
+
Robust error handling throughout:
|
|
369
|
+
|
|
370
|
+
```python
|
|
371
|
+
from schemez.code_generation import ToolCodeGenerator
|
|
372
|
+
|
|
373
|
+
# Schema-only generator (no execution capability)
|
|
374
|
+
generator = ToolCodeGenerator.from_schema(schema)
|
|
375
|
+
|
|
376
|
+
try:
|
|
377
|
+
# This will fail with clear error message
|
|
378
|
+
generator.add_route_to_app(app)
|
|
379
|
+
except ValueError as e:
|
|
380
|
+
print(e) # "Callable required for route generation for tool 'my_function'"
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## Use Cases
|
|
384
|
+
|
|
385
|
+
- **AI Tool Integration**: Convert functions to OpenAI-compatible tools
|
|
386
|
+
- **API Client Generation**: Create type-safe HTTP clients from schemas
|
|
387
|
+
- **FastAPI Automation**: Auto-generate routes with validation
|
|
388
|
+
- **Documentation**: Generate comprehensive API docs
|
|
389
|
+
- **Testing**: Create mock implementations and test data
|
|
390
|
+
- **Code Analysis**: Extract and analyze function signatures
|
|
391
|
+
- **Dynamic Execution**: Build sandboxed Python environments
|
|
392
|
+
|
|
393
|
+
## Differences from Pydantic
|
|
394
|
+
|
|
395
|
+
While Pydantic focuses on detailed type preservation, schemez optimizes for practical AI interaction:
|
|
396
|
+
|
|
397
|
+
- **Simplified unions**: Takes first type instead of complex anyOf schemas
|
|
398
|
+
- **Enum flattening**: Extracts enum values as simple string arrays
|
|
399
|
+
- **AI-optimized**: Generates schemas that work well with LLM function calling
|
|
400
|
+
- **Code generation focus**: Built for generating executable code, not just validation
|
|
401
|
+
|
|
402
|
+
## Contributing
|
|
403
|
+
|
|
404
|
+
Contributions welcome! This library consolidates schema and type utilities from multiple projects into a unified toolkit.
|