Skip to content
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

ChatBedrock doesn't support streaming when Bedrock guardrails are applied #176

Closed
ardf opened this issue Sep 2, 2024 · 7 comments
Closed

Comments

@ardf
Copy link
Contributor

ardf commented Sep 2, 2024

Description

When using ChatBedrock with streaming enabled and Bedrock guardrails applied, the streaming functionality breaks. This appears to be due to differences in the response structure when guardrails are active.

Steps to Reproduce

  1. Set up a ChatBedrock instance with streaming enabled.
  2. Configure and apply Bedrock guardrails to the model.
  3. Attempt to use the streaming functionality.

Current Behavior

The streaming functionality fails with a KeyError, specifically:

\venv\Lib\site-packages\langchain_aws\llms\bedrock.py", line 148, in _stream_response_to_generation_chunk
    |     "stop_sequence": stream_response["delta"]["stop_sequence"],
    |                      ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
    | KeyError: 'stop_sequence'

This occurs in the _stream_response_to_generation_chunk method of the llms\bedrock.py file.

Expected Behavior

The streaming functionality should work correctly regardless of whether guardrails are applied or not.

Additional Context

  • The application works fine without guardrails and streaming responses are received as expected.
  • The issue only occurs when Bedrock guardrails are applied to the ChatBedrock instance.

Environment

  • langchain_aws version: 0.1.17
  • Python version: 3.12.2
  • Operating System: Windows 11

Possible Solution

The _stream_response_to_generation_chunk method in bedrock.py should be updated to handle cases where the response structure differs when guardrails are applied. This may involve checking for the existence of certain keys before accessing them, or handling different response structures based on whether guardrails are active.

@langcarl langcarl bot added the investigate label Sep 2, 2024
@langcarl langcarl bot assigned ccurme Sep 2, 2024
ardf added a commit to ardf/langchain-aws that referenced this issue Sep 2, 2024
@john-mihailescu
Copy link

I am also seeing this issue. In addition - ChatBedrock still does not support Guardrails configurations, which are necessary to properly do prompt attack filtering via input tagging and to filter responses asynchronously.
There is already a PR open here for this issue that several people are waiting for - #59

@ardf
Copy link
Contributor Author

ardf commented Sep 5, 2024

I am also seeing this issue. In addition - ChatBedrock still does not support Guardrails configurations, which are necessary to properly do prompt attack filtering via input tagging and to filter responses asynchronously.
There is already a PR open here for this issue that several people are waiting for - #59

Well, AFAIK, it does support bedrock guardrails, however streaming has to be disabled for the same. The problem occurs when streaming is enabled.

I've migrated to ChatBedrockConverse for my project, which will eventually replace ChatBedrock class.

@dillycone
Copy link

I am also seeing this issue. In addition - ChatBedrock still does not support Guardrails configurations, which are necessary to properly do prompt attack filtering via input tagging and to filter responses asynchronously.
There is already a PR open here for this issue that several people are waiting for - #59

Well, AFAIK, it does support bedrock guardrails, however streaming has to be disabled for the same. The problem occurs when streaming is enabled.

I've migrated to ChatBedrockConverse for my project, which will eventually replace ChatBedrock class.

Does ChatBedrockConverse fix it?

@ardf
Copy link
Contributor Author

ardf commented Sep 28, 2024

I am also seeing this issue. In addition - ChatBedrock still does not support Guardrails configurations, which are necessary to properly do prompt attack filtering via input tagging and to filter responses asynchronously.
There is already a PR open here for this issue that several people are waiting for - #59

Well, AFAIK, it does support bedrock guardrails, however streaming has to be disabled for the same. The problem occurs when streaming is enabled.

I've migrated to ChatBedrockConverse for my project, which will eventually replace ChatBedrock class.

Does ChatBedrockConverse fix it?

Yes, I'm able to stream the response using ChatBedrockConverse with guardrails configured.

@dillycone
Copy link

Thank you.

@rpgeddam
Copy link

is ChatBedrockConverse a drop in replacement for ChatBedrock?

@3coins 3coins added the bedrock label Oct 9, 2024
3coins pushed a commit that referenced this issue Oct 9, 2024
This PR addresses an issue where the streaming functionality in
ChatBedrock breaks when Bedrock guardrails are applied. The problem was
caused by a KeyError in the `_stream_response_to_generation_chunk`
method when trying to access the 'stop_sequence' key, which may not
always be present in the response structure when guardrails are active.

## Changes
- Modified the `_stream_response_to_generation_chunk` method in
`llms/bedrock.py` to use the `get()` method when accessing dictionary
keys that may not always be present.


## Testing
- Tested the streaming functionality with and without Bedrock guardrails
applied.
- Verified that the streaming works correctly in both scenarios without
raising KeyErrors.
@3coins
Copy link
Collaborator

3coins commented Oct 9, 2024

Fixed by #177

@3coins 3coins closed this as completed Oct 9, 2024
ardf added a commit to ardf/langchain-aws that referenced this issue Oct 10, 2024
langchain-ai#177)

This PR addresses an issue where the streaming functionality in
ChatBedrock breaks when Bedrock guardrails are applied. The problem was
caused by a KeyError in the `_stream_response_to_generation_chunk`
method when trying to access the 'stop_sequence' key, which may not
always be present in the response structure when guardrails are active.

## Changes
- Modified the `_stream_response_to_generation_chunk` method in
`llms/bedrock.py` to use the `get()` method when accessing dictionary
keys that may not always be present.


## Testing
- Tested the streaming functionality with and without Bedrock guardrails
applied.
- Verified that the streaming works correctly in both scenarios without
raising KeyErrors.
3coins pushed a commit that referenced this issue Oct 11, 2024
…ardrails (#176)  (#231)

This PR addresses an issue where the streaming functionality in
ChatBedrock breaks when Bedrock guardrails are applied. The problem was
caused by a KeyError in the `_stream_response_to_generation_chunk`
method when trying to access the 'stop_sequence' key, which may not
always be present in the response structure when guardrails are active.

## Changes
- Modified the `_stream_response_to_generation_chunk` method in
`llms/bedrock.py` to use the `get()` method when accessing dictionary
keys that may not always be present.


## Testing
- Tested the streaming functionality with and without Bedrock guardrails
applied.
- Verified that the streaming works correctly in both scenarios without
raising KeyErrors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants