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

using JsonOutputParser with ChatOpenAI seems to be breaking streaming and returning only one chunk #7026

Open
5 tasks done
praveenkvr opened this issue Oct 19, 2024 · 3 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@praveenkvr
Copy link

praveenkvr commented Oct 19, 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

When i switch from JsonOutputParser to StringOutputParser string works fine. Let me know if i am using the API wrong thanks!.

Error Message and Stack Trace (if applicable)

No error but just get a single chunk when i stream with JsonOutputParser

Description

I have the following code

const prompt = new PromptTemplate({
  template,
  inputVariables: ["text"],
  partialVariables: { format_instructions: parser.getFormatInstructions() },
});

const model = new ChatOpenAI({
  model: "gpt-4o",
  temperature: 0,
  apiKey: process.env.OPENAI_API_KEY,
});

const chain = prompt.pipe(model).pipe(new JsonOutputParser());

const stR = await chain.stream({ idea });
for await (const item of stR) {
  console.log(item);
}

I tried the exact same in python and streaming works there where i got multiple chunks of partial JSON responses back

parser = JsonOutputParser(pydantic_object=InputFormat)

prompt = PromptTemplate(
    template=template,
    input_variables=['text'],
    partial_variables={"format_instructions": parser.get_format_instructions() }
)

model = ChatOpenAI(model='gpt-4o', temperature=0, api_key=os.getenv("OPENAI_API_KEY"))

chain = prompt | model | parser

for chunk in chain.stream({ "idea": idea}):
    print(chunk, end='', flush=True)
    print('\n')

When i switch from JsonOutputParser to StringOutputParser string works fine. Let me know if i am using the API wrong thanks!.

System Info

Using the latest version of langchain code (0.3.13) and openAi

Copy link

dosubot bot commented Oct 19, 2024

Hey @praveenkvr! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together!

The issue you're experiencing with the JsonOutputParser returning only one chunk in streaming mode might be related to how the parser handles streaming data. The JsonOutputParser is designed to process streamed JSON by incrementally building up the JSON object as tokens are received. However, any steps in the chain that operate on finalized inputs rather than on input streams can break streaming functionality [1].

Here are a few things you can check or try:

  1. Ensure Streaming Compatibility: Make sure that all components in your chain are compatible with streaming. If any part of the chain processes data in a way that requires the complete input before producing output, it can disrupt the streaming process.

  2. Check for Finalized Input Operations: Avoid any operations in the chain that require finalized inputs. For example, if you have any functions or methods that expect the entire JSON object to be available before processing, it will prevent streaming from working correctly.

  3. Use the Correct Parser: If the StringOutputParser works fine, it indicates that the issue might be specific to how the JsonOutputParser is handling the data. Ensure that the JsonOutputParser is correctly configured to handle streaming JSON data.

If these suggestions don't resolve the issue, it might be worth reviewing the specific implementation details of your chain to ensure that all components are set up to handle streaming data correctly.

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Oct 19, 2024
@jacoblee93
Copy link
Collaborator

This looks like a bug, but we generally recommend that people use .withStructuredOutput instead with newer models:

https://js.langchain.com/docs/how_to/structured_output/

Can you try that?

@AshotN
Copy link

AshotN commented Nov 11, 2024

I can confirm that withStructuredOutput works for streaming chunks. But JsonOutputParser (which is the what the docs recommend) doesn't.

StructuredOutputParser also does not seem to work correctly

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

No branches or pull requests

3 participants