Repository navigation
find_context_parameter treats a Context return annotation as a parameter #3298
Description
Activity
Hi, I'd like to work on this. I'll submit a focused PR with regression coverage shortly.
I noticed the contribution guide asks contributors to wait for maintainer feedback or a ready-for-work signal before implementation. I'm stepping back for now so this can be triaged first.
So I ran into this and dug in a bit. The cause is that
get_type_hints()hands back the return annotation under a"return"key alongside the real parameters, andfind_context_parameterloops over the whole mapping, so a handler annotated-> Contextends up recorded as having a context parameter literally named"return". Sincereturnis a keyword, injecting it can never work: the handler registers fine and then blows up the first time it's called.One thing that surprised me is that it isn't just tools. Prompts and resource templates resolve their context through the same function, so all three break the same way.
Fix is to drop the
"return"key before scanning the hints. I've put up a PR with that plus regression tests, I'd be happy to close it if you'd rather take a different approach.Disclosure: I used AI assistance (Claude Code) on this. I read through the change myself and can explain it.
- addedv1Affects the v1.x maintenance lineAffects the v1.x maintenance linev2Affects the v2 line (2.x on main)Affects the v2 line (2.x on main)
on Aug 18, 2026 Reproduced on current main: find_context_parameter returns 'return' for 'def fn(value: int) -> Context', expected None. A normal 'ctx: Context' parameter still resolves correctly.
The cause is that typing.get_type_hints() includes the return annotation under the key 'return', which is not a parameter. Skipping that key fixes it, including for Optional[Context] returns.
I have a minimal fix plus a test ready and would be glad to open a PR if you are open to an outside contribution here.
I've put together a fix for this along with tests (the module previously had no direct test coverage): Drima-code@f37be04
The change is minimal —
find_context_parameter()skips the"return"key thattyping.get_type_hints()injects for the return annotation, since it is the only non-parameter key the map can contain. Happy to open a PR whenever it's appropriate to do so.
Initial checks
mainbranch (6e30452).Description
find_context_parameter()iterates over every entry returned bytyping.get_type_hints(). That mapping includes the function return annotation under the special key"return", even though it is not a parameter.As a result, a tool, prompt, or resource function whose return annotation is
Context(or a union containingContext) is incorrectly recorded as having a context parameter named"return". The SDK later attempts to inject context as a keyword argument with that name, which can produce an unexpected-keyword error when the callable is invoked.Expected: context detection only examines actual function parameters and ignores the return annotation.
Example code
Environment
mainat6e30452I used AI assistance while investigating this issue and reviewed and reproduced the behavior locally.