Skip to content

Bug: BedrockAgentFunctionResolver keeps app.context when a tool raises #8873

Open

Description

@hyeonsang010716

Expected Behaviour

app.context is cleared at the end of every invocation, including when the tool raises. The next invocation in the same execution environment starts with an empty context. ApiGatewayResolver (#8538) and AppSyncResolver (#8584) already work this way.

Current Behaviour

BedrockAgentFunctionResolver._resolve() calls clear_context() only after the tool returns. If the tool raises, the except Exception branch returns the Error: ... response without clearing the context. The next warm invocation then sees the data the failed invocation appended, which can belong to another caller.

The same happens when resolve() raises ValueError("Missing required field: ...") for an invalid event: context appended before resolve() is never cleared.

Code snippet

from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver

app = BedrockAgentFunctionResolver()


@app.tool(name="charge")
def charge(user: str):
    app.append_context(user=user)
    if user == "alice":
        raise RuntimeError("payment backend down")
    return f"charged {user}"


@app.tool(name="whoami")
def whoami():
    return {"context_seen": dict(app.context)}


def event(function: str, parameters: list) -> dict:
    return {
        "messageVersion": "1.0",
        "agent": {"alias": "a", "name": "n", "version": "1", "id": "i"},
        "sessionId": "s",
        "actionGroup": "g",
        "function": function,
        "parameters": parameters,
        "inputText": "",
        "sessionAttributes": {},
        "promptSessionAttributes": {},
    }


app.resolve(event("charge", [{"name": "user", "type": "string", "value": "alice"}]), None)
print(app.resolve(event("whoami", []), None)["response"]["functionResponse"]["responseBody"])

Output:

{'TEXT': {'body': '{"context_seen": {"user": "alice"}}'}}

Expected output: {'TEXT': {'body': '{"context_seen": {}}'}}

Possible Solution

Clear the context in a finally block in resolve(), the same fix used in #8539 for ApiGatewayResolver and in #8629 for AppSyncResolver, and remove the success-only clear_context() call in _resolve().

Steps to Reproduce

  1. Register a tool that calls app.append_context(...) and then raises.
  2. Invoke it through app.resolve(...).
  3. Invoke any other tool on the same resolver instance and read app.context.

Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

3.41.0

AWS Lambda function runtime

3.14

Packaging format used

PyPi

Debugging logs

Activity

  1. boring-cyborg commented on Nov 19, 2026

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  2. powertools-for-aws-oss-automation commented on Nov 19, 2026

    @powertools-for-aws-oss-automation

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out our GitHub Discussions: Join the conversation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions