Skip to content

[BUG] opt/python/opentelemetry on lambda #195

Open
@rpostulart

Description

@rpostulart

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

0.1.1

Python Version

3.11

Operating System

Mac

Installation Method

pip

Steps to Reproduce

When deploying strands via lambda I get this error

I tried pyhton 3.11 and 3.12

{
  "errorMessage": "",
  "errorType": "StopIteration",
  "requestId": "",
  "stackTrace": [
    "  File \"/var/lang/lib/python3.11/importlib/__init__.py\", line 126, in import_module\n    return *bootstrap.*gcd_import(name[level:], package, level)\n",
    "  File \"<frozen importlib._bootstrap>\", line 1204, in *gcd*import\n",
    "  File \"<frozen importlib._bootstrap>\", line 1176, in *find*and_load\n",
    "  File \"<frozen importlib._bootstrap>\", line 1147, in *find*and_load_unlocked\n",
    "  File \"<frozen importlib._bootstrap>\", line 690, in *load*unlocked\n",
    "  File \"<frozen importlib._bootstrap_external>\", line 940, in exec_module\n",
    "  File \"<frozen importlib._bootstrap>\", line 241, in *call*with_frames_removed\n",
    "  File \"/var/task/lambda_function.py\", line 8, in <module>\n    from strands import Agent\n",
    "  File \"/opt/python/strands/__init__.py\", line 3, in <module>\n    from . import agent, event_loop, models, telemetry, types\n",
    "  File \"/opt/python/strands/agent/__init__.py\", line 9, in <module>\n    from .agent import Agent\n",
    "  File \"/opt/python/strands/agent/agent.py\", line 22, in <module>\n    from opentelemetry import trace\n",
    "  File \"/opt/python/opentelemetry/trace/__init__.py\", line 85, in <module>\n    from opentelemetry import context as context_api\n",
    "  File \"/opt/python/opentelemetry/context/__init__.py\", line 70, in <module>\n    *RUNTIME*CONTEXT = *load*runtime_context()\n",
    "  File \"/opt/python/opentelemetry/context/__init__.py\", line 60, in *load*runtime_context\n    return next(  # type: ignore\n"
  ]
}
[ERROR]	2025-06-09T19:54:11.118Z		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
[ERROR] StopIteration
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return *bootstrap.*gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in *gcd*import
  File "<frozen importlib._bootstrap>", line 1176, in *find*and_load
  File "<frozen importlib._bootstrap>", line 1147, in *find*and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in *load*unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in *call*with_frames_removed
  File "/var/task/lambda_function.py", line 8, in <module>
    from strands import Agent
  File "/opt/python/strands/__init__.py", line 3, in <module>
    from . import agent, event_loop, 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 22, in <module>
    from opentelemetry import trace
  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: 695.93 ms	Phase: init	Status: error	Error Type: Runtime.Unknown
[ERROR]	2025-06-09T19:54:11.735Z		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
[ERROR] StopIteration
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return *bootstrap.*gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in *gcd*import
  File "<frozen importlib._bootstrap>", line 1176, in *find*and_load
  File "<frozen importlib._bootstrap>", line 1147, in *find*and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in *load*unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in *call*with_frames_removed
  File "/var/task/lambda_function.py", line 8, in <module>
    from strands import Agent
  File "/opt/python/strands/__init__.py", line 3, in <module>
    from . import agent, event_loop, 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 22, in <module>
    from opentelemetry import trace
  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: 595.41 ms	Phase: invoke	Status: error	Error Type: Runtime.Unknown
START RequestId: 5cca313d-b16a-4cbd-8423-188319c7073b Version: $LATEST
END RequestId: 5cca313d-b16a-4cbd-8423-188319c7073b
REPORT RequestId: 5cca313d-b16a-4cbd-8423-188319c7073b	Duration: 605.35 ms	Billed Duration: 606 ms	Memory Size: 2000 MB	Max Memory Used: 74 MB	Status: error	Error Type: Runtime.Unknown


I have this in my requirements.txt
strands-agents
strands-agents-tools
boto3
requests
pydantic

This is the script to build the zip file for lambda:

  $DOCKER_CMD run --rm \
        --entrypoint="" \
        -v "$PWD":/var/task \
        -w /var/task \
        public.ecr.aws/lambda/python:3.11 \
        /bin/bash -c "
            echo '=== Installing zip utility ==='
            yum update -y && yum install -y zip
            
            echo '=== Installing shared infrastructure dependencies ==='
            rm -rf python/
            mkdir python
            
            echo 'Contents of requirements.txt:'
            cat requirements.txt
            
            echo 'Installing packages with verbose output:'
            pip install -v -r requirements.txt -t python/
            
            echo '=== Checking installed packages ==='
            ls -la python/ | head -10
            echo 'Total packages installed:' \$(ls python/ | wc -l)
            
            echo '=== Looking for key modules ==='
            find python/ -name '*boto*' -type d | head -5
            find python/ -name '*aws*' -type d | head -5
            
            echo '=== Testing imports ==='
            cd python
            python3 -c 'import boto3; print(\"✅ boto3 import successful\")' || echo '❌ boto3 import failed'
            python3 -c 'import botocore; print(\"✅ botocore import successful\")' || echo '❌ botocore import failed'
            cd ..
            
            echo '=== Creating zip file ==='
            zip -r shared-infrastructure-layer.zip python/ -x '*.pyc' '*/__pycache__/*' '*/tests/*' '*.dist-info/*'
            echo 'Shared infrastructure layer build complete!'
            ls -la shared-infrastructure-layer.zip
        

Expected Behavior

Lambda invokes as expected

Actual Behavior

Error, see above

Additional Context

No response

Possible Solution

No response

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions