Skip to content

feat(event_handler): Ensure Bedrock Agents resolver works with Pydantic v2 #5156

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 1 commit into from
Sep 11, 2024

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #4196

Summary

Changes

This PR addresses the compatibility issue between Bedrock Agents and the OpenAPI schemas generated by Pydantic v2. Bedrock Agents only accepts OpenAPI version 3.0, whereas Pydantic v2 generates schemas that are valid for OpenAPI 3.1.

Changes

  • Added functionality to convert OpenAPI 3.1 schemas to 3.0 compatible versions
  • Enforced OpenAPI 3.0.3 schema output for Bedrock Agent compatibility
  • Override get_openapi_json_schema method in the BedrockAgentResolver class to handle the conversion process and make it strict to BedrockAgent resolver.

User experience

from time import time
from typing import Optional

from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import BedrockAgentResolver
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger()
app = BedrockAgentResolver()


@app.get("/current_time", description="Gets the current time in seconds")
def current_time() -> Optional[int]:
    return int(time())


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


if __name__ == "__main__": 
    print(app.get_openapi_json_schema())

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.

@leandrodamascena leandrodamascena requested a review from a team September 11, 2024 14:24
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 11, 2024
@github-actions github-actions bot added the feature New feature or functionality label Sep 11, 2024
Copy link

@sthulb sthulb merged commit 9acfee4 into aws-powertools:v3 Sep 11, 2024
17 checks passed
@leandrodamascena leandrodamascena linked an issue Sep 11, 2024 that may be closed by this pull request
2 tasks
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
Development

Successfully merging this pull request may close these issues.

Maintenance: Ensure Bedrock Agents resolver works with Pydantic v2
2 participants