Skip to content

O(N²) performance bottleneck in [MessageStream](src/lib/MessageStream.ts#51-747) during structured output streaming #933

@Emeenent14

Description

@Emeenent14

Description:

### Summary
During streaming of structured outputs (JSON), `MessageStream` re-parses the entire cumulative JSON buffer for every single delta received. For long responses, this leads to **O(N²)** complexity, causing high CPU usage and UI freezing.

### Root Cause
In `MessageStream.ts`, `newContent.input` is eagerly calculated on every delta using `partialParse(jsonBuf)`.

### Proposed Fix
Convert `newContent.input` into a lazy getter. This ensures `partialParse` is only executed when the user or a listener actually accesses the field, reducing redundant work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions