-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Currently, the function schema utility does not support useful additional properties such as maximum
, minimum
, and max_length
in the generated JSON schemas for Python functions. This feature enhancement would provide greater flexibility and precision in schema definitions.
Additional Context
Before implementing this enhancement, it is necessary to review how to specify additional properties in Annotations to ensure a consistent and user-friendly approach.
Suggestion 1. Consider dict argument as additional properties
user_id: Annotated[str, Doc("Username of service"), {"min_length": 5, "pattern": "a-za-z0-9_{4,}"}]
Pros: easy
Cons: Break if any tool that was using dict in them for any other purpose
Suggestion 2. Pydantic-like approach
num_images: Annotated[int, Doc("Number of images to generate"), FieldInfo(gte=1, lte=4)]
Pros: Familiar with who are using Pydantic already.
Cons: Make things complex.
Suggestion 3. Magic keyword
from function_schema import FieldInfo as F
def create_image(
prompt: Annotated[
str,
Doc(
"Detailed prompt for generate image that contains"
" camera angle, style, emotional moods, and color tones"
),
F.length > 20,
],
num_images: Annotated[int, Doc("Number of images to generate"), 1 <= F <= 4] = 1,
user_id: Annotated[
Optional[str],
Doc("Username for audit"),
F.length < 20,
F.pattern("a-za-z0-9_{4,}"),
] = None,
): ...
Pros:
Consistency: Provides a consistent way to specify additional properties across different functions and modules.
Readability: Enhances code readability by clearly specifying constraints and properties directly in the function signature.
Validation: Facilitates automatic validation of input parameters based on the specified properties.
Documentation: Serves as in-line documentation, making it easier for developers to understand the expected constraints and properties of function parameters.
Cons:
Complexity: Increases the complexity of the function signature, which may make it harder to read and understand for newcomers.
Learning Curve: Requires developers to learn and understand the specific syntax and usage of the magic keywords.
Error Handling: May introduce subtle bugs if the magic keywords are not used correctly or if there are conflicts with existing annotations.
Performance: Potentially impacts performance due to the additional processing required to interpret and validate the magic keywords.