Check that keyword arguments match a schema before dispatching a function call.
The full prompt is available without an account.
Implement validate_and_call(schema, fn, kwargs) that validates keyword arguments
against a JSON-like tool schema before calling the function.
The schema dict has this shape:
{"type": "object", "properties": {"name": {"type": "string"}, ...}, "required": ["name"]}
Rules:
- Every key listed in schema["required"] must appear in kwargs.
- Every key in kwargs must appear in schema["properties"] (no unexpected keys).
- Raise ValueError with a descriptive message for any violation.
- If validation passes, call fn(**kwargs) and return the result.
Read-only Python 3.12 preview. Sign in to edit and execute it.
from typing import Callable
def validate_and_call(schema: dict, fn: Callable, kwargs: dict):
# TODO: validate then call
return fn(**kwargs)