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

Ambiguous Type Check in @langchain/aws #6101

Closed
5 tasks done
wailinkyaww opened this issue Jul 17, 2024 · 3 comments · Fixed by #6137
Closed
5 tasks done

Ambiguous Type Check in @langchain/aws #6101

wailinkyaww opened this issue Jul 17, 2024 · 3 comments · Fixed by #6137
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@wailinkyaww
Copy link

wailinkyaww commented Jul 17, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

This is from @langchain/aws.

Link to source code -

if (contentBlockDelta.delta.text) {

if (contentBlockDelta.delta.text) {
    return new ChatGenerationChunk({
      text: contentBlockDelta.delta.text,
      message: new AIMessageChunk({
        content: contentBlockDelta.delta.text,
      }),
    });
}

Error Message and Stack Trace (if applicable)

Error Message

TypeError: undefined is not an object (evaluating 'unsupportedField[0][0]')

Stack Trace

The error is coming from -

const unsupportedField = Object.entries(contentBlockDelta.delta).filter(

Description

  • I am trying to integrate ChatBedrockConverse from @langchain/aws with streaming.
  • Sometimes, the language model produces empty string as a token which will cause contentBlockDelta.delta.text to be an empty string.
  • As we are checking the text using truthy/falsy behaviour of JS, the chunk doesn't fall into the first if block.

This delta with an empty string ( text ) will goes into the else block.

const unsupportedField = Object.entries(contentBlockDelta.delta).filter(

In line 401, we are again retrieving the unsupportedField by checking the existence of the value.
This will also fail because it is an empty string which will cause unsupportedField to be empty array.

Basically our error handler is throwing the error here.


Resolution

I suggest we use the stricter type check instead of checking the existence of text from detla in L370.

// instead of
if (contentBlockDelta.delta.text) { }

// use string type check
if (typeof contentBlockDelta.delta.text === 'string') {}

I believe the library shouldn't care about what kind of tokens are passing through. As long as it is a valid string, we shouldn't care.

System Info

Package => @langchain-aws - ^0.0.2
Package manager => happens in both yarn and bun ( because it doesn't matter )

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jul 17, 2024
Copy link

dosubot bot commented Jul 17, 2024

Hey @wailinkyaww! I'm here to help you out with any bugs, questions, or contributions you have in mind. Let's tackle this together while we wait for a human maintainer.

To address the issue with the ambiguous type check in the @langchain/aws package, you can modify the common.ts file to use a stricter type check for the text field. This will prevent errors caused by empty strings. Here's how you can update the relevant parts of the code:

  1. Update the handleConverseStreamContentBlockDelta function to include a stricter type check for the text field:
export function handleConverseStreamContentBlockDelta(
  contentBlockDelta: ContentBlockDeltaEvent
): ChatGenerationChunk {
  if (!contentBlockDelta.delta) {
    throw new Error("No delta found in content block.");
  }
  if (typeof contentBlockDelta.delta.text === 'string' && contentBlockDelta.delta.text.trim() !== "") {
    return new ChatGenerationChunk({
      text: contentBlockDelta.delta.text,
      message: new AIMessageChunk({
        content: contentBlockDelta.delta.text,
      }),
    });
  } else if (contentBlockDelta.delta.toolUse) {
    const index = contentBlockDelta.contentBlockIndex;
    return new ChatGenerationChunk({
      text: "",
      message: new AIMessageChunk({
        content: "",
        tool_call_chunks: [
          {
            args: contentBlockDelta.delta.toolUse.input,
            index,
          },
        ],
      }),
    });
  } else {
    const unsupportedField = Object.entries(contentBlockDelta.delta).filter(
      ([_, value]) => !!value
    );
    throw new Error(
      `Unsupported content block type: ${unsupportedField[0][0]}`
    );
  }
}

This change ensures that the text field is checked to be a non-empty string, preventing issues with empty strings causing errors [1].

To continue talking to Dosu, mention @dosu.

@jacoblee93
Copy link
Collaborator

Oof - will patch quickly, thanks for reporting!

@bracesproul
Copy link
Member

@wailinkyaww version 0.0.4 is now released with the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants