Repository navigation
Description
Description
A @generative stub whose return type is imported only under if TYPE_CHECKING: (or is otherwise unresolvable at runtime) raises NameError at decoration time. Because the decorator runs at import, the containing module cannot be imported at all — this is not a degraded prompt, it is a hard import failure.
Reproducer
Verified against main (72488902):
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from decimal import Decimal
from mellea import generative
@generative
def estimate_price(item: str) -> Decimal:
"""Estimate the price of an item."""
...File ".../inspect.py", line 292, in get_annotations
value if not isinstance(value, str) else eval(value, globals, locals)
File "<string>", line 1, in <module>
NameError: name 'Decimal' is not defined
No model call is involved. Importing the module is enough.
Root cause
create_response_format resolves annotations with get_type_hints(func), which evaluates the whole signature including the return annotation. Under from __future__ import annotations every annotation is a string, so a return type that only exists for type checkers becomes a runtime NameError — even when every parameter resolves cleanly. GenerativeStub.__init__ calls it, which is why the failure lands at decoration time.
This is not a regression from #1504. get_type_hints entered create_response_format in 88a4d754 ("feat: rename generative slots -> generative stubs", #801), and the decoration failure has existed since. Verified by checking out a535fc63, the parent of #1504, where the reproducer above raises the identical NameError from the same get_type_hints frame.
What #1504 did change is two lines — describe_function and get_argument switching to inspect.signature(func, eval_str=True). That makes direct calls to those two functions raise on an unresolvable return annotation where they previously returned a quoted string. No @generative user could observe it, because create_response_format already failed earlier in __init__. So #1504 is a narrow regression for direct callers only, and not the cause of the bug in the title.
Three call sites in mellea/stdlib/components/genstub.py are affected, and they differ in whether the return annotation is actually needed:
| Site | Reads the return annotation? |
|---|---|
create_response_format (get_type_hints, since #801) |
Yes — it types the Pydantic response model that parses model output |
describe_function (eval_str=True, since #1504) |
Yes — the rendered signature goes into the prompt via GenerativeStub.jinja2 |
get_argument (eval_str=True, since #1504) |
No — only the named parameter's annotation is read |
create_response_format is the one that breaks decoration, since GenerativeStub.__init__ calls it.
This is not rare: 84 of 1319 callables across mellea and cli currently fail inspect.signature(..., eval_str=True), so any of those used as a @generative return type triggers it.
Expected behaviour
Decoration should succeed. Parameter annotations should still resolve. An unresolvable return type should degrade rather than raise:
create_response_formatshould fall back toAnyfor the response model — the same fallback an unannotated function already receives viatype_hints.get("return", Any).describe_functionshould leave the annotation postponed so the prompt still names the type (-> 'Decimal') rather than omitting it.get_argumentshould be unaffected, since it never reads the return annotation.
Resolvable annotations must keep the #1503 behaviour exactly — no quoted parameter annotations in the prompt.
Note
This is fixed in #1509, which was already fixing the same root cause in convert_function_to_ollama_tool (#1507) and shares a helper for the resolution logic. Filed for traceability, since #1509's original scope covered only the backends half.
Activity
fix(stdlib): resolve genstub annotations without failing on TYPE_CHEC…
fix: resolve postponed annotations without failing on TYPE_CHECKING r…