Try an interactive version of this dialog: Sign up at solve.it.com, click Upload, and pass this URL.

Code: 156 ()

from dataclasses import dataclass, field, asdict
from typing import Optional
from lisette import *

@dataclass
class Adult:
    "How do adults supposed to help in this game"
    adult_role: str  # "facilitator" | "demonstrator" | "player" | "observer"
    adults_required: int
    losing_gracefully: Optional[bool]  # Can adult elegantly lose? None if not competitive

t = lite_mk_func(Adult)
t

Output: 607

{'type': 'function',
 'function': {'name': 'Adult',
  'description': 'How do adults supposed to help in this game',
  'parameters': {'type': 'object',
   'properties': {'adult_role': {'type': 'string', 'description': ''},
    'adults_required': {'type': 'integer', 'description': ''},
    'losing_gracefully': {'type': 'object',
     'description': '',
     'anyOf': [{'type': 'boolean'}, {'type': 'null'}]}},
   'title': 'Adult',
   'required': ['adult_role', 'adults_required', 'losing_gracefully']}}}

Code: 150 ()

from typing import Annotated
from dataclasses import dataclass

@dataclass
class Adult:
    "How do adults supposed to help in this game"
    adult_role: Annotated[str, "facilitator | demonstrator | player | observer"]
    adults_required: Annotated[int, "Number of adults needed"]
    losing_gracefully: Annotated[Optional[bool], "Can adult elegantly lose? None if not competitive"]
    
t = lite_mk_func(Adult)
t

Output: 522

{'type': 'function',
 'function': {'name': 'Adult',
  'description': 'How do adults supposed to help in this game',
  'parameters': {'type': 'object',
   'properties': {'adult_role': {'type': 'object', 'description': ''},
    'adults_required': {'type': 'object', 'description': ''},
    'losing_gracefully': {'type': 'object', 'description': ''}},
   'title': 'Adult',
   'required': ['adult_role', 'adults_required', 'losing_gracefully']}}}

Code: 822 ()

from typing import get_origin, get_args, Annotated
from inspect import Parameter
import toolslm
from toolslm.funccall import _is_container, _is_parameterized, _handle_container, _handle_type, _types
from typing import get_origin, get_args, Annotated
from inspect import Parameter

def _param(name, info, evalable=False):
    "json schema parameter given `name` and `info` from docments full dict"
    anno = info.anno
    desc = info.docment or ""
    
    # Handle Annotated[T, "description"]
    if get_origin(anno) is Annotated:
        args = get_args(anno)
        anno = args[0]  # unwrap to base type
        if len(args) > 1 and isinstance(args[1], str):
            desc = args[1]
    
    paramt, itemt = _types(anno)
    pschema = dict(type=paramt, description=desc)
    if itemt: pschema["items"] = {"type": itemt}
    if info.default is not Parameter.empty:
        if evalable:
            try: ast.literal_eval(repr(info.default))
            except: pschema["default"] = str(info.default)
            else: pschema["default"] = info.default
        else: pschema["default"] = info.default
    return pschema

def _process_property(name, obj, props, req, defs, evalable=False):
    "Process a single property of the schema"
    anno = obj.anno
    desc_override = None
    
    # Unwrap Annotated to get base type and optional description
    if get_origin(anno) is Annotated:
        args = get_args(anno)
        anno = args[0]
        if len(args) > 1 and isinstance(args[1], str):
            desc_override = args[1]
    
    p = _param(name, obj, evalable=evalable)
    if desc_override:
        p['description'] = desc_override
    props[name] = p
    if obj.default is Parameter.empty: req[name] = True

    # Use `anno` instead of `obj.anno` from here on
    if _is_container(anno) and _is_parameterized(anno):
        p.update(_handle_container(get_origin(anno), get_args(anno), defs))        
    else:
        p.update(_handle_type(anno, defs))


toolslm.funccall._param = _param 
toolslm.funccall._process_property = _process_property 
t = lite_mk_func(Adult)
t

Output: 652

{'type': 'function',
 'function': {'name': 'Adult',
  'description': 'How do adults supposed to help in this game',
  'parameters': {'type': 'object',
   'properties': {'adult_role': {'type': 'string',
     'description': 'facilitator | demonstrator | player | observer'},
    'adults_required': {'type': 'integer',
     'description': 'Number of adults needed'},
    'losing_gracefully': {'type': 'object',
     'description': 'Can adult elegantly lose? None if not competitive',
     'anyOf': [{'type': 'boolean'}, {'type': 'null'}]}},
   'title': 'Adult',
   'required': ['adult_role', 'adults_required', 'losing_gracefully']}}}

Code: 0