Skip to content

Cannot return a 4xx status code from BedrockAgentCoreApp entrypoint #284

@markeramzn

Description

@markeramzn

Describe the bug
Only status code 200 or 500 can be sent to a client when you use an entrypoint decorated function to handle requests for a BedrockAgentCoreApp. There's no way to send a status code in the 400s to indicate invalid user input.

To Reproduce
Run this server and send a request to the /invocations endpoint with body {}

from bedrock_agentcore.runtime import BedrockAgentCoreApp
from starlette.exceptions import HTTPException

app = BedrockAgentCoreApp()

@app.entrypoint
def invoke(payload):
    prompt = payload.get("prompt")
    if prompt is not None:
        return {"message": "a message"}
    else:
        raise HTTPException(status_code=400, detail="Prompt missing")

if __name__ == "__main__":
    app.run()

Expected behavior
Server should respond with a 400 status code to the client but instead it responds with a 500 status code.

Version (consider adding this to the template)
0.1.0

Additional context
Starlette supports either throwing an HTTPException with a status code or returning an instance of a Response or Response subclass like JSONResponse with a status code. Neither method works here because the endpoint function's return value gets converted to JSON and wrapped in a JSONResponse with status code 200 and exceptions are caught and replaced by the framework.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions