fix(core): await coroutines returned by callable token getters and bound params#736
Open
hsusul wants to merge 1 commit into
Open
fix(core): await coroutines returned by callable token getters and bound params#736hsusul wants to merge 1 commit into
hsusul wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem & Minimal Reproduction
In
toolbox-core,resolve_valueis responsible for resolving dynamic sources (such asauth_service_token_getters,bound_params, andclient_headers) into values before invoking remote tools or building client headers.The public type annotations and docstrings explicitly support
Callable[[], Awaitable[Any]]:source: Union[Callable[[], Any], Callable[[], Awaitable[Any]], Any]However, when a callable is not a raw
async deffunction (for example: a callable class instance withasync def __call__(self), a lambda returning an awaitable likelambda: get_token_async(), or a function returning a coroutine),asyncio.iscoroutinefunction(source)evaluates toFalse.When
resolve_valueexecuteselif callable(source): return source(),source()returns anAwaitable(coroutine object) which is never awaited.resolve_valuereturns<coroutine object ...>instead of the resolved value string/object, emitting aRuntimeWarning: coroutine '...' was never awaitedand causing transport header or payload errors (TypeError/ invalid header format).Minimal Reproduction:
Current vs. Corrected Behavior
Awaitablecausesresolve_valueto return an unawaited<coroutine object ...>, triggering a runtime warning and failing when constructing headers or payloads.resolve_valueinspects the return value of any callable and awaits it ifinspect.isawaitable(res)isTrue, ensuring all coroutines/awaitables are resolved to their underlying values.Root Cause
resolve_valuerelied solely onasyncio.iscoroutinefunction(source)to determine if anawaitwas required. In Python, callable objects implementingasync def __call__, lambdas returning awaitables,functools.partialwrappers, and functions returning coroutines do not satisfyasyncio.iscoroutinefunction(source).Minimal Implementation
Updated
resolve_valueinpackages/toolbox-core/src/toolbox_core/utils.py:Tests
Added
test_resolve_value_callable_returning_awaitabletopackages/toolbox-core/tests/test_utils.pyverifying that callable class instances withasync def __call__and lambdas returning awaitables resolve to their expected string values without runtime warnings.Validation Results
toolbox-core: 479 passed in 0.81s (pytest -k "not e2e")toolbox-adk: 54 passed in 0.73s (pytest tests/unit)toolbox-langchain: 60 passed in 0.20s (pytest -k "not e2e")toolbox-llamaindex: 61 passed in 0.77s (pytest -k "not e2e")black --check .: Passedisort --check .: Passedmypy: Passed across all 4 package modules (toolbox_core,toolbox_adk,toolbox_langchain,toolbox_llamaindex)git diff --check: CleanAdapter Compatibility
All framework adapters (
toolbox-adk,toolbox-langchain,toolbox-llamaindex) delegate token resolution and bound parameters totoolbox-core'sresolve_value. This fix restores proper async token resolution for custom callable objects and lambdas across all adapters.Issue Creation Limitation
No standalone GitHub issue was created prior to opening this PR because issue creation permissions on
googleapis/mcp-toolbox-sdk-pythonare restricted to repository maintainers.Explicit Non-Goals