Skip to content

Conversation

@Ansarafsar
Copy link

@Ansarafsar Ansarafsar commented Nov 27, 2025

Description

Adds an optional LangChain integration as an extra dependency. Users can install with pip install "toon-python[langchain]" and use from toon_format.langchain import ToonSerializer to convert Documents to compact TOON format, saving 30-60% tokens in RAG and agent prompts.

Type of Change

  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test coverage improvement

Related Issues

Closes # (no issue yet, this is the first LangChain request)

Changes Made

  • Added src/toon_format/langchain/ with ToonSerializer and ToonOutputParser
  • Added tests/test_langchain.py with 2 tests
  • Added [langchain] extra in pyproject.toml
  • Updated README.md with installation and usage example

SPEC Compliance

  • This PR implements/fixes spec compliance

Testing

  • All existing tests pass
  • Added new tests for changes
  • Tested on Python 3.8
  • Tested on Python 3.9
  • Tested on Python 3.10
  • Tested on Python 3.11
  • Tested on Python 3.12

Test Output

============================= test session starts =============================
collected 94 items

tests/test_langchain.py::test_toon_serializer_basic PASSED              [ 98%]
tests/test_langchain.py::test_toon_output_parser PASSED                 [100%]
... (all other 92 tests passed)

========================== 94 passed in 0.85s ==========================

Code Quality

  • Ran ruff check src/toon_format tests - no issues
  • Ran ruff format src/toon_format tests - code formatted
  • Ran mypy src/toon_format - no critical errors
  • All tests pass: pytest tests/ -v

Checklist

  • My code follows the project's coding standards (PEP 8, line length 100)
  • I have added type hints to new code
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation (README.md, CLAUDE.md if needed)
  • My changes do not introduce new dependencies
  • I have maintained Python 3.8+ compatibility
  • I have reviewed the TOON specification for relevant sections

Performance Impact

  • No performance impact

Breaking Changes

  • No breaking changes

Screenshots / Examples

# Example usage
from toon_format.langchain import ToonSerializer
from langchain_core.documents import Document

docs = [Document(page_content={"name": "Ansar", "city": "Salem"})]
result = ToonSerializer().transform_documents(docs)
print(result[0].page_content)

Output:

name:Ansar,city:Salem

Additional Context

This is a fully optional integration. Core users are unaffected. After the next release, LangChain developers can save significant tokens in prompts with just one line of code.

Checklist for Reviewers

  • Code changes are clear and well-documented
  • Tests adequately cover the changes
  • Documentation is updated
  • No security concerns
  • Follows TOON specification
  • Backward compatible (or breaking changes are justified and documented)

@Ansarafsar Ansarafsar requested review from a team and johannschopplich as code owners November 27, 2025 18:24
@Ansarafsar
Copy link
Author

@johannschopplich

PR ready for review!

  • Fully optional LangChain integration ([langchain] extra)
  • ToonSerializer → converts retrieved Documents to compact TOON (30–60 % token savings)
  • ToonOutputParser → parses TOON responses back to Python objects
  • Sync + async support
  • 2 passing tests + README example
  • Zero impact on core users
  • All CI checks passed locally (ruff, mypy, pytest)

This is the #1 requested feature for RAG/agent developers who want cheaper prompts.

Happy to add benchmarks, more tests, or a docs page if needed — just let me know!

Thanks for the quick review

Ansarafsar and others added 2 commits November 28, 2025 09:42
Co-authored-by: Johann Schopplich <[email protected]>
Added LangChain integration details and usage examples.
Copy link
Contributor

@johannschopplich johannschopplich left a comment

Choose a reason for hiding this comment

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

Thanks for the updates LGTM (README-wise). As for the code and usefulness, @toon-format/python-maintainers shall decide. :)

@Ansarafsar
Copy link
Author

Ansarafsar commented Nov 28, 2025 via email

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.

2 participants