Skip to content

feat: add compact API support for OpenAI#34926

Closed
constance (jiaming2li) wants to merge 6 commits intolangchain-ai:masterfrom
jiaming2li:feat/openai-compact-api
Closed

feat: add compact API support for OpenAI#34926
constance (jiaming2li) wants to merge 6 commits intolangchain-ai:masterfrom
jiaming2li:feat/openai-compact-api

Conversation

@jiaming2li
Copy link
Copy Markdown
Contributor

Summary: This PR introduces support for OpenAI's new /responses/compact endpoint. This allows for efficient context management by compacting message history directly through the API.
close #34924

Changes:

Added use_compact_api and compact_target_tokens parameters to ChatOpenAI.

Implemented routing logic in _generate and _agenerate to use the compact endpoint when requested.

Added _construct_compact_result to parse the specific response format of the compact API.

- Add tokens_per_image parameter for fixed image token penalty
- Handle list-based content blocks (text, image, image_url)
- Prevent massive overestimation from base64-encoded images
- Maintain backward compatibility with string content
- Add comprehensive test coverage for multimodal scenarios

Fixes token counting for multimodal messages where base64 images
were previously counted as 25,000+ tokens instead of ~85 tokens.
- Add tokens_per_image parameter for fixed image token penalty
- Handle list-based content blocks (text, image, image_url)
- Prevent massive overestimation from base64-encoded images
- Maintain backward compatibility with string content
- Add comprehensive test coverage for multimodal scenarios
@github-actions github-actions bot added external integration PR made that is related to a provider partner package integration core `langchain-core` package issues & PRs openai `langchain-openai` package issues & PRs labels Jan 30, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Jan 30, 2026

Merging this PR will not alter performance

✅ 6 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing jiaming2li:feat/openai-compact-api (82eb70a) with master (9a2a10e)

Open in CodSpeed

Footnotes

  1. 29 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@mdrxy
Copy link
Copy Markdown
Member

constance (@jiaming2li) would you mind resolving all CI failures and conflicts?

@jiaming2li
Copy link
Copy Markdown
Contributor Author

constance (@jiaming2li) would you mind resolving all CI failures and conflicts?

I‘ll fix them tomorrow.

@github-actions github-actions bot added langchain `langchain` package issues & PRs langchain-classic `langchain-classic` package issues & PRs labels Jan 31, 2026
@jiaming2li constance (jiaming2li) changed the title Feat/OpenAI compact api feat: add compact API support for OpenAI Jan 31, 2026
@github-actions github-actions bot added the feature For PRs that implement a new feature; NOT A FEATURE REQUEST label Jan 31, 2026
@pedrojrv
Copy link
Copy Markdown

Hello, this is great! any updates on what it takes to get this merged?

@jiaming2li
Copy link
Copy Markdown
Contributor Author

Hello, this is great! any updates on what it takes to get this merged?

Hi! Thanks for the follow-up. I have just updated the branch with the latest master, and it is now ready to be merged.
Please let me know if you need anything else!

Copy link
Copy Markdown
Collaborator

@ccurme ccurme (ccurme) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thanks very much for this.

ChatOpenAI now supports automatic server-side compaction via #35212.

There may still be reason to support "manual" compaction (direct use through the compact endpoints), but we'd need a design doc for this. The implementation here has a number of problems:

  • We need to instantiate a separate ChatOpenAI(use_compact_api=True) to use it. It may make more sense as a compact method.
  • We return an artificial AIMessage with content "[Conversation compacted]". There's no reason to add this content to the message history. We need end-to-end usage examples showing how to use model.compact (or similar) in a multi-turn conversation.

If #35212 is insufficient and we do need support for manual compaction, would you mind opening an issue explaining the use case and proposing a design?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external feature For PRs that implement a new feature; NOT A FEATURE REQUEST integration PR made that is related to a provider partner package integration langchain `langchain` package issues & PRs langchain-classic `langchain-classic` package issues & PRs openai `langchain-openai` package issues & PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support OpenAI responses/compact endpoint

4 participants