You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4, because the PR involves multiple complex changes including refactoring, error handling, and retry logic, which require careful review to ensure correctness and robustness.
🧪 Relevant tests
Yes
⚡ Possible issues
Retry Logic Concern: The retry logic for handling expired tokens might not correctly reinitialize the client if the session token remains expired or invalid. This could lead to repeated failures without proper handling of the new token acquisition.
Exception Handling: The broad exception handling in several methods could mask specific errors, making debugging more difficult. It's generally better to catch more specific exceptions where possible.
🔒 Security concerns
No
Code feedback:
relevant file
semantic_router/encoders/bedrock.py
suggestion
Consider adding a backoff strategy for the retry logic to handle API rate limits or network issues more gracefully. This can prevent the system from being overwhelmed by repeated quick retries. [important]
It's recommended to handle specific exceptions from the Boto3 and Botocore libraries instead of a general exception, which can help in identifying the root cause of failures more accurately. [important]
To improve the security and robustness of token handling, consider implementing a method to refresh the session token automatically when it expires, instead of relying on the environment variable that might not update in real-time. [important]
Refactor the chunk_strings method to handle edge cases where the text might not need chunking or when the chunks are smaller than MAX_WORDS. This can prevent unnecessary processing and potential errors. [medium]
Instead of catching a general Exception, specify the types of exceptions that are expected during client initialization. This will help in better error handling and debugging.
-except Exception as e:+except (SpecificExceptionType1, SpecificExceptionType2) as e:
Suggestion importance[1-10]: 8
Why: Using specific exceptions improves error handling and debugging, making the code more robust and easier to maintain. This is a best practice that enhances code quality significantly.
8
Enhancement
Replace hardcoded retry delay with a configurable variable
Replace the hardcoded retry delay with a variable that can be adjusted based on the environment or configuration settings. This will make the retry mechanism more flexible and maintainable.
Why: This suggestion improves the flexibility and maintainability of the retry mechanism by allowing the retry delay to be configurable. However, it is not a critical change and does not address a major bug or issue.
7
Make the MAX_WORDS parameter adjustable in the chunk_strings method
The method chunk_strings uses a hardcoded MAX_WORDS value for chunking, which might not be optimal for all scenarios. Consider making MAX_WORDS a parameter of the method to allow flexibility.
Why: Making MAX_WORDS adjustable adds flexibility to the chunk_strings method, which can be beneficial for different use cases. However, it is a minor enhancement and does not address any critical issues.
6
Maintainability
Refactor error handling in retry logic for clarity
Refactor the error handling in the retry logic to separate concerns and improve readability. Specifically, handle the token expiration and other client errors in different blocks or methods.
if error.response["Error"]["Code"] == "ExpiredTokenException":
- logger.warning("Session token has expired. Retrying initialisation.")- try:- self.session_token = os.getenv("AWS_SESSION_TOKEN")- self.client = self._initialize_client(- self.access_key_id,- self.secret_access_key,- self.session_token,- self.region,- )- except Exception as e:- raise ValueError(f"Bedrock client failed to reinitialise. Error: {e}") from e+ handle_token_expiration()+else:+ handle_client_error(error)
Suggestion importance[1-10]: 7
Why: This refactoring improves code readability and maintainability by separating concerns in the error handling logic. While it enhances the structure of the code, it is not addressing a critical issue.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Type
Bug fix, Enhancement, Tests, Documentation
Description
BedrockEncoder
.BedrockEncoder
.BedrockEncoder
.BedrockEncoder
.BedrockEncoder
, including retry logic, chunking functionality, and various initialization scenarios.botocore
inpyproject.toml
.Changes walkthrough 📝
bedrock.py
Fix and enhance BedrockEncoder initialization and API calls
semantic_router/encoders/bedrock.py
test_bedrock.py
Add comprehensive tests for BedrockEncoder
tests/unit/encoders/test_bedrock.py
pyproject.toml
Update dependencies to include botocore
pyproject.toml
botocore
as an optional dependency.bedrock
extra to includebotocore
.