FG
💻 Software🤖 AI & LLMsOpenAI

Model can generate enormous amounts of whitespace/newlines and then the structured output is truncated

Fresh3 days ago
Mar 14, 20260 views
Confidence Score50%
50%

Problem

Confirm this is a Node library issue and not an underlying OpenAI API issue - [X] This is an issue with the Node library Describe the bug When using `json_schema`, sometimes the model outputs thousands of newlines. At some point it reaches the max output length and stops, causing it to sometimes return a non valid json object (it didn't finish writing it). Because of how this library is structured, the developer can't see the problem (the output is not logged). I've found that including [code block] will solve this. To Reproduce happens randomly when using `json_schema` Code snippets _No response_ OS irrelevant Node version irrelevant Library version 4.71.1

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Output Trimming to Prevent Excess Whitespace in JSON Responses

Medium Risk

The Node library is generating excessive whitespace and newlines due to improper handling of output formatting when using the `json_schema`. This leads to the model exceeding the maximum output length, resulting in truncated and invalid JSON objects. The library does not log output, making it difficult for developers to identify the issue.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Add Output Trimming Functionality

    Create a function that trims excessive whitespace and newlines from the model's output before it is processed or returned. This will help ensure that the output is valid and within the acceptable length limits.

    javascript
    function trimOutput(output) { return output.replace(/\n+/g, '\n').trim(); }
  2. 2

    Integrate Trimming Function in Output Handling

    Modify the part of the code where the model's output is received to include the trimming function. This ensures that any output generated by the model is processed through the trimming function before further use.

    javascript
    const processedOutput = trimOutput(modelOutput);
  3. 3

    Implement Logging for Debugging

    Add logging functionality to capture the model's output before and after trimming. This will help in debugging and ensure that the trimming process is effective.

    javascript
    console.log('Raw Output:', modelOutput); console.log('Trimmed Output:', processedOutput);
  4. 4

    Test the Implementation

    Run multiple tests using `json_schema` to confirm that the output is consistently valid JSON and that excessive whitespace is eliminated. Ensure that the output length does not exceed the maximum limit.

    javascript
    // Example test case
    const testOutput = trimOutput('   \n\n\n{ "key": "value" }   \n\n'); console.assert(testOutput === '{ "key": "value" }', 'Output is not valid JSON');

Validation

Confirm that the output from the model is consistently valid JSON without excessive whitespace or newlines. Additionally, check the logs to ensure that the trimming function is being applied correctly.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

openaigptllmapiopenai-api