Skip to content

fix: allow custom HTTP status codes from entrypoint handlers (#284)#296

Merged
aidandaly24 merged 2 commits intomainfrom
fix/entrypoint-custom-status-codes-284
Mar 4, 2026
Merged

fix: allow custom HTTP status codes from entrypoint handlers (#284)#296
aidandaly24 merged 2 commits intomainfrom
fix/entrypoint-custom-status-codes-284

Conversation

@aidandaly24
Copy link
Contributor

@aidandaly24 aidandaly24 commented Mar 4, 2026

fix: allow custom HTTP status codes from entrypoint handlers (#284)

Issue

Closes #284

@app.entrypoint handlers could only produce HTTP 200 (success) or 500 (exception). There was no way to return 4xx status codes for invalid input because:

  1. All return values were serialized and wrapped in Response(200)
  2. All exceptions (including HTTPException) were caught and converted to JSONResponse(500)

Changes

Two mechanisms for handlers to control HTTP status codes:

1. Raise HTTPException (the approach from the issue):

from starlette.exceptions import HTTPException

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

2. Return a Response object directly:

from starlette.responses import JSONResponse

@app.entrypoint
def invoke(payload):
   if not payload.get("prompt"):
       return JSONResponse({"error": "Prompt missing"}, status_code=400)
   return {"message": "ok"}

Both were mentioned in the issue as expected behavior.

Logging improvements

  • HTTPException with 4xx → logs at WARNING
  • HTTPException with 5xx → logs at ERROR (matches generic exception severity)
  • Returning a Response with 4xx/5xx → logs at WARNING instead of "Invocation completed successfully"

What's unchanged

  • Returning a dict/list/string → still wrapped in Response(200)
  • Unhandled exceptions → still JSONResponse(500)
  • Invalid JSON body → still JSONResponse(400)
  • Streaming responses → unaffected

Testing

Verified with 5 scenarios:

Scenario Expected Result
raise HTTPException(400) 400
Return dict normally 200
Return JSONResponse(404) 404
raise HTTPException(500) 500
raise ValueError() 500

@aidandaly24 aidandaly24 requested a review from a team March 4, 2026 16:53

# If handler returned a Starlette Response directly, pass it through.
# This lets handlers control status codes (e.g. JSONResponse(data, status_code=404)).
if isinstance(result, Response):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be part of the if, elif statement above.

@aidandaly24 aidandaly24 merged commit 2371461 into main Mar 4, 2026
20 checks passed
@aidandaly24 aidandaly24 deleted the fix/entrypoint-custom-status-codes-284 branch March 4, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot return a 4xx status code from BedrockAgentCoreApp entrypoint

2 participants