Skip to content

feat(bedrock_agents): add optional fields to response payload #6336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 30, 2025

Conversation

anafalcao
Copy link
Contributor

@anafalcao anafalcao commented Mar 24, 2025

Issue number:

Summary

Changes

This PR introduces the new BedrockResponse class to support custom Amazon Bedrock Agent responses.

  1. BedrockResponse class with support for:
    • Session attributes
    • Prompt session attributes
    • Knowledge base configurations
  2. Type-safe implementation with proper validation
  3. Example showing how to use BedrockResponse with Bedrock Agents in the documentation

User experience

from http import HTTPStatus

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import BedrockAgentResolver
from aws_lambda_powertools.event_handler.api_gateway import BedrockResponse
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = BedrockAgentResolver()


@app.get("/return_with_session", description="Returns a hello world with session attributes")
@tracer.capture_method
def hello_world():
    return BedrockResponse(
        status_code=HTTPStatus.OK.value,
        body={"message": "Hello from Bedrock!"},
        session_attributes={"user_id": "123"},
        prompt_session_attributes={"context": "testing"},
        knowledge_bases_configuration=[
            {
                "knowledgeBaseId": "kb-123",
                "retrievalConfiguration": {
                    "vectorSearchConfiguration": {"numberOfResults": 3, "overrideSearchType": "HYBRID"},
                },
            },
        ],
    )


@logger.inject_lambda_context
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext):
    return app.resolve(event, context)

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation event_handlers tests labels Mar 24, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 24, 2025
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Mar 24, 2025
@anafalcao anafalcao self-assigned this Mar 24, 2025
Copy link

codecov bot commented Mar 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.12%. Comparing base (8883b1e) to head (0d2921b).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6336   +/-   ##
========================================
  Coverage    96.11%   96.12%           
========================================
  Files          253      253           
  Lines        12086    12104   +18     
  Branches       898      902    +4     
========================================
+ Hits         11617    11635   +18     
  Misses         369      369           
  Partials       100      100           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Mar 25, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Mar 25, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Mar 26, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Mar 26, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Mar 26, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Mar 26, 2025
@anafalcao anafalcao marked this pull request as ready for review March 26, 2025 13:35
@anafalcao anafalcao requested a review from a team as a code owner March 26, 2025 13:35
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Mar 27, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Mar 27, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 3, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 3, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 16, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 16, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 29, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 29, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 29, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 29, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 29, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 29, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 29, 2025
@leandrodamascena leandrodamascena removed their request for review April 29, 2025 21:16
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 29, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 30, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 30, 2025
@anafalcao
Copy link
Contributor Author

Hey @dreamorosi, can you check this PR?

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 30, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 30, 2025
@leandrodamascena leandrodamascena merged commit 8463fb9 into develop Apr 30, 2025
14 checks passed
@leandrodamascena leandrodamascena deleted the response branch April 30, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
3 participants