Skip to content

Commit 9d875d6

Browse files
authored
feat: add better error messages for incorrect genslot args (#248)
1 parent 1194059 commit 9d875d6

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

mellea/stdlib/genslot.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,17 @@ def bind_function_arguments(
204204
Dictionary mapping parameter names to bound values with defaults applied.
205205
"""
206206
signature = inspect.signature(func)
207-
bound_arguments = signature.bind(*args, **kwargs)
207+
try:
208+
bound_arguments = signature.bind(*args, **kwargs)
209+
except TypeError as e:
210+
# Provide a clear error message when parameters from the original function are missing
211+
if "missing" in str(e) and "required" in str(e):
212+
raise TypeError(
213+
f"generative slot is missing required parameter(s) from the original function '{func.__name__}': {e}"
214+
) from e
215+
216+
# Else re-raise the error if it's not the expected error.
217+
raise e
208218
bound_arguments.apply_defaults()
209219
return dict(bound_arguments.arguments)
210220

@@ -346,10 +356,22 @@ def _context_backend_extract_args_and_kwargs(
346356
using_session_overload = True
347357

348358
# Call the appropriate function and let python handle the arg/kwarg extraction.
349-
if using_session_overload:
350-
extracted = _session_extract_args_and_kwargs(*args, **kwargs)
351-
else:
352-
extracted = _context_backend_extract_args_and_kwargs(*args, **kwargs)
359+
try:
360+
if using_session_overload:
361+
extracted = _session_extract_args_and_kwargs(*args, **kwargs)
362+
else:
363+
extracted = _context_backend_extract_args_and_kwargs(*args, **kwargs)
364+
except TypeError as e:
365+
# Provide a clear error message when required mellea parameters are missing
366+
if "missing" in str(e) and (
367+
"context" in str(e) or "backend" in str(e) or "m" in str(e)
368+
):
369+
raise TypeError(
370+
"generative slot requires either a MelleaSession (m=...) or both a Context and Backend (context=..., backend=...) to be provided as the first argument(s)"
371+
) from e
372+
373+
# If it's not the expected err, simply re-raise it.
374+
raise e
353375

354376
if len(extracted.f_args) > 0:
355377
raise TypeError(

0 commit comments

Comments
 (0)