Skip to content

Add append operation for efficient text streaming #170

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeonsworld
Copy link

Summary

This PR adds a new append operation to python-json-patch for efficient text streaming in chat applications. When streaming text character-by-character or word-by-word, the standard replace operation requires sending the entire string with each update. The new append operation only sends the incremental text, significantly reducing payload size and improving performance.
This method is used by official ChatGPT and is an implementation optimized for chat.

Key Features

  • New append operation: Appends text to existing string values
  • Optimized notation support: Reduces payload size for consecutive operations
  • Smart patch generation: make_patch() automatically detects append scenarios
  • Full backward compatibility: Maintains RFC 6902 compliance

Implementation Details

  1. AppendOperation class: New operation type that appends text to strings at specified JSON paths
    {"op": "append", "path": "/message/content", "value": "Hello"}
  1. Optimized notation for streaming:
    - Standard: {"op": "append", "path": "/message", "value": "text"}
    - Short: {"p": "/message", "o": "append", "v": "text"}
    - optimized: {"v": "text"} (reuses previous path/operation)
  1. patch generation: When using make_patch(), the library now detects when a string change is an append
    operation:
  src = {"message": "Hello"}
  dst = {"message": "Hello World"}
  patch = jsonpatch.make_patch(src, dst)
  # Generates: [{"op": "append", "path": "/message", "value": " World"}]
  # Instead of: [{"op": "replace", "path": "/message", "value": "Hello World"}]

Performance

The demo script shows append operations are ~2.8x faster than replace operations for streaming scenarios.

…cations

- Add new AppendOperation class for appending text to string values
- Support optimized notation: short form (p/o/v) and optimized (v only)
- Enhance DiffBuilder to detect append scenarios and generate append ops instead of replace
- Add comprehensive test coverage for append functionality
- Include demo script showing streaming performance improvements

The append operation enables efficient real-time text streaming for chat interfaces by avoiding full string replacements. Consecutive append operations can omit path and operation fields for minimal payload size.
This is how it is actually used in [ChatGPT](https://chatgpt.com/)

Example:
```json
{"op": "append", "path": "/message", "value": "Hello"}
{"v": " World"}  // Optimized continuation
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant