strands-agents/sdk-python

[BUG] Lambda runtime error "Failed to load context: contextvars_context"

Closed this issue · 3 comments

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

v1.13.0

Python Version

3.12.v89

Operating System

Linux (AWS Lambda)

Installation Method

pip

Steps to Reproduce

  1. Create a simple strands agent that runs on AWS Lambda
  2. Invoke lambda

Expected Behavior

Lambda invoke should succeed.

Actual Behavior

Lambda invoke fails.

Problem is present with both lambda architectures: "x86_64" and "arm64".

Error logs:

[ERROR] Failed to load context: contextvars_context, fallback to contextvars_context Traceback (most recent call last): File "/opt/python/opentelemetry/context/__init__.py", line 46, in _load_runtime_context return next( # type: ignore ^^^^^^^^^^^^^^^^^^^^^ StopIteration

LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html

[ERROR] StopIteration Traceback (most recent call last): File "/var/lang/lib/python3.12/importlib/__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 999, in exec_module File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "/var/task/lambda.py", line 8, in <module> from strands import Agent File "/opt/python/strands/__init__.py", line 3, in <module> from . import agent, models, telemetry, types File "/opt/python/strands/agent/__init__.py", line 9, in <module> from .agent import Agent File "/opt/python/strands/agent/agent.py", line 31, in <module> from opentelemetry import trace as trace_api File "/opt/python/opentelemetry/trace/__init__.py", line 85, in <module> from opentelemetry import context as context_api File "/opt/python/opentelemetry/context/__init__.py", line 70, in <module> _RUNTIME_CONTEXT = _load_runtime_context() File "/opt/python/opentelemetry/context/__init__.py", line 60, in _load_runtime_context return next( # type: ignore

INIT_REPORT Init Duration: 286.39 ms Phase: init Status: error Error Type: Runtime.Unknown

Hi,

I tried with the example in our documentation with --platform --platform manylinux2014_aarch64, and I can see the response and the statusCode is 200:

{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}

Could you clarify if there are any other changes on your end in the code so we can reproduce it? Did you enable tracer and saw this exception?

Hi,

that seems to do the trick, thanks for swift response.

I will close this.

Best, Ari