Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow schema generation for functions with pydantic model and UUID arguments #92

Closed
shouples opened this issue Sep 30, 2023 · 0 comments · Fixed by #93
Closed

allow schema generation for functions with pydantic model and UUID arguments #92

shouples opened this issue Sep 30, 2023 · 0 comments · Fixed by #93
Assignees

Comments

@shouples
Copy link
Collaborator

shouples commented Sep 30, 2023

  • ChatLab version: 1.0.0-alpha.25
  • Python version: 3.10.12
  • Operating System: Windows 11 + WSL (Ubuntu 22.04)

Description

I'm testing various functions and methods with generate_function_schema() and running into situations where the JSON serialization check is raising an exception. Details below:

What I Did

With pydantic model argument:

from pydantic import BaseModel

class SimpleModel(BaseModel):
    abc: str
    xyz: int

def sample_function_with_model(
    foo: str,
    bar: int,
    baz: SimpleModel
) -> None:
    """test"""
    print(f"{foo=}, {bar=}, {baz=}")

schema = generate_function_schema(sample_function_with_model)
schema
     10     """test"""
     11     print(f"{foo=}, {bar=}, {baz=}")
---> 13 schema = generate_function_schema(sample_function_with_model)
     14 schema

File ~/dev/.venv/lib/python3.10/site-packages/chatlab/registry.py:160, in generate_function_schema(function, parameter_schema)
    158 sig = inspect.signature(function)
    159 for name, param in sig.parameters.items():
--> 160     prop_schema, is_required = process_parameter(name, param)
    161     schema_properties[name] = prop_schema
    162     if is_required:

File ~/dev/.venv/lib/python3.10/site-packages/chatlab/registry.py:128, in process_parameter(name, param)
    126 def process_parameter(name, param):
    127     """Process a function parameter for use in a JSON schema."""
--> 128     prop_schema, is_required = process_type(param.annotation, param.default == inspect.Parameter.empty)
    129     if param.default != inspect.Parameter.empty:
    130         prop_schema["default"] = param.default

File ~/dev/.venv/lib/python3.10/site-packages/chatlab/registry.py:123, in process_type(annotation, is_required)
    118     return {
    119         "type": JSON_SCHEMA_TYPES[annotation],
    120     }, is_required
    122 else:
--> 123     raise Exception(f"Type annotation must be a JSON serializable type ({ALLOWED_TYPES})")

Exception: Type annotation must be a JSON serializable type ([<class 'int'>, <class 'str'>, <class 'bool'>, <class 'float'>, <class 'list'>, <class 'dict'>, typing.List, typing.Dict])

With uuid argument:

import uuid

def sample_function_with_uuid(
    foo: str,
    bar: int,
    baz: uuid.UUID
) -> None:
    """test"""
    print(f"{foo=}, {bar=}, {baz=}")

schema = generate_function_schema(sample_function_with_uuid)
schema
      8     """test"""
      9     print(f"{foo=}, {bar=}, {baz=}")
---> 11 schema = generate_function_schema(sample_function_with_uuid)
     12 schema

File ~/dev/.venv/lib/python3.10/site-packages/chatlab/registry.py:160, in generate_function_schema(function, parameter_schema)
    158 sig = inspect.signature(function)
    159 for name, param in sig.parameters.items():
--> 160     prop_schema, is_required = process_parameter(name, param)
    161     schema_properties[name] = prop_schema
    162     if is_required:

File ~/dev/.venv/lib/python3.10/site-packages/chatlab/registry.py:128, in process_parameter(name, param)
    126 def process_parameter(name, param):
    127     """Process a function parameter for use in a JSON schema."""
--> 128     prop_schema, is_required = process_type(param.annotation, param.default == inspect.Parameter.empty)
    129     if param.default != inspect.Parameter.empty:
    130         prop_schema["default"] = param.default

File ~/dev/.venv/lib/python3.10/site-packages/chatlab/registry.py:123, in process_type(annotation, is_required)
    118     return {
    119         "type": JSON_SCHEMA_TYPES[annotation],
    120     }, is_required
    122 else:
--> 123     raise Exception(f"Type annotation must be a JSON serializable type ({ALLOWED_TYPES})")

Exception: Type annotation must be a JSON serializable type ([<class 'int'>, <class 'str'>, <class 'bool'>, <class 'float'>, <class 'list'>, <class 'dict'>, typing.List, typing.Dict])

Expected schema:

{
        "name": "sample_function_with_uuid",
        "description": "test",
        "parameters": {
            "type": "object",
            "properties": {
                "foo": {"type": "string"},
                "bar": {"type": "integer"},
                "baz": {"type": "string", "format": "uuid"},
            },
            "required": ["foo", "bar", "baz"],
        },
    }
@shouples shouples self-assigned this Sep 30, 2023
@shouples shouples changed the title allow schema generation for functions with pydantic model arguments and UUIDs allow schema generation for functions with pydantic model and UUID arguments Sep 30, 2023
@shouples shouples linked a pull request Sep 30, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant