Skip to content

Add AST-aware code chunking for better code understanding#58

Merged
yichuan-w merged 13 commits into
StarTrail-org:mainfrom
gabriel-dehan:feature/integrate-astchunk
Aug 20, 2025
Merged

Add AST-aware code chunking for better code understanding#58
yichuan-w merged 13 commits into
StarTrail-org:mainfrom
gabriel-dehan:feature/integrate-astchunk

Conversation

@gabriel-dehan

@gabriel-dehan gabriel-dehan commented Aug 16, 2025

Copy link
Copy Markdown
Contributor

Introduces intelligent AST-aware code chunking to LEANN using astchunk, improving code understanding in RAG applications. Instead of arbitrarily splitting code at character boundaries, we now have the option to preserve semantic structure by chunking at function, class, and method boundaries.

This enhancement should make LEANN significantly better for code-related RAG applications while maintaining full backward compatibility.

I am also planning on adding ruby support to astchunk and have opened a pull request there.

What's new?

AST-Aware Code Chunking

  • Smart boundary detection: Preserves functions, classes, and methods as complete units
  • Multi-language support: Python, Java, C#, TypeScript out of the box (Ruby soon #PR_LINK)
  • Graceful fallback: Falls back to traditional chunking for unsupported languages or parsing errors
  • Rich metadata: Each chunk includes line numbers, file paths, and AST node information

📱 New application

  • Code RAG app (apps/code_rag.py): Specialized for code repositories with optimized parameters
  • Enhanced document RAG: New --enable-code-chunking flag for mixed content

🛠️ CLI Integration

  • Global --use-ast-chunking flag for the main CLI
  • Configurable chunk sizes and overlap for both code and text
  • Proper error handling and user feedback

How this helps

Before: Code was chunked arbitrarily, often splitting functions mid-way

# Traditional chunking might split this:
def important_function():
    """This docstring explains the function."""
    # Implementation here...
--- CHUNK BOUNDARY ---
    return result

After: AST chunking preserves semantic boundaries

# AST chunking keeps this together:
def important_function():
    """This docstring explains the function."""
    # Complete implementation
    return result

This should result in a much better code understanding and search quality in RAG applications.

Examples

# Enable AST chunking for mixed content
python -m apps.document_rag --enable-code-chunking --data-dir ./my_project

# Use the specialized code RAG
python -m apps.code_rag --repo-dir ./codebase --query "How does authentication work?"

# Global CLI with AST support
leann build my-index --docs ./src --use-ast-chunking

Files Changed

  • apps/chunking_utils.py - New AST-aware chunking utilities
  • apps/code_rag.py - Specialized code RAG application
  • apps/base_rag_example.py - Enhanced with AST chunking arguments
  • apps/document_rag.py - Added code chunking flag
  • packages/leann-core/src/leann/cli.py - CLI integration
  • pyproject.toml - Added astchunk dependencies
  • tests/test_astchunk_integration.py - Comprehensive test suite
  • README.md & docs/features.md - Updated documentation

Dependencies

Added optional dependencies for AST parsing:

  • astchunk>=0.1.0 - Core AST chunking library
  • tree-sitter>=0.20.0 + language parsers - AST parsing backend

PS: Hopefully I didn't break anything during the rebase with main, I haven't tested everything after rebasing but the basic things are still working as expected.

@yichuan-w
yichuan-w requested review from Copilot and yichuan-w August 16, 2025 04:49
@yichuan-w yichuan-w self-assigned this Aug 16, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces AST-aware code chunking to LEANN using the astchunk library, enhancing code understanding in RAG applications by preserving semantic boundaries like functions and classes instead of arbitrary text splits.

Key changes include:

  • AST-aware chunking utilities that detect code files and apply semantic chunking
  • Enhanced document RAG with --enable-code-chunking flag for mixed content
  • New specialized code RAG application optimized for code repositories

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_document_rag.py Adds test for document RAG with AST chunking enabled
tests/test_astchunk_integration.py Comprehensive test suite for AST chunking functionality
pyproject.toml Adds astchunk and tree-sitter dependencies
packages/leann-core/src/leann/cli.py Integrates AST chunking flags and logic into main CLI
docs/features.md Documents new AST-aware code chunking feature
apps/document_rag.py Adds code chunking support to document RAG
apps/code_rag.py New specialized application for code repository RAG
apps/chunking/utils.py Core AST chunking utilities and fallback mechanisms
apps/chunking/__init__.py Package initialization for chunking utilities
apps/base_rag_example.py Adds AST chunking parameters to base RAG class
README.md Updates documentation with code chunking examples
ASTCHUNK_INTEGRATION.md Comprehensive integration documentation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread apps/code_rag.py
Comment thread apps/code_rag.py Outdated
Comment thread apps/chunking/utils.py Outdated
@yichuan-w
yichuan-w requested a review from andylizf August 16, 2025 04:55
@andylizf
andylizf removed the request for review from yichuan-w August 16, 2025 04:55
@yichuan-w

Copy link
Copy Markdown
Collaborator

Thanks for your contribution!! It is a very important feature, we will review and merge asap.

@yichuan-w

Copy link
Copy Markdown
Collaborator

We recently came across astchunk, the pkg you are using and its accompanying paper, too. Have you had a chance to evaluate it or compare it against naive chunking? I’m not entirely convinced about the extent of its performance gains, but it could be worth discussing further.

@andylizf andylizf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Mostly LGTM! Thanks for your hard work @gabriel-dehan ! Left some discussions.

Comment thread apps/code_rag.py

from base_rag_example import BaseRAGExample, create_text_chunks
from llama_index.core import SimpleDirectoryReader

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
from .utils import CODE_EXTENSIONS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This wouldn't work as we are not in the same directory no? We've got an init.py in chunking/ so chunking should be correct (or chunking.utils, but not .utils)
I've left it as is

Comment thread ASTCHUNK_INTEGRATION.md Outdated
print(f"⚠️ AST chunking not available ({e}), falling back to traditional chunking")
use_ast = False

if not use_ast:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if not use_ast:
else:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually no because we want to fallback if there is an error (see line right above, we set use_ast = False) so the next if needs to be separated and not an alternative branch of the previous condition!

Comment thread packages/leann-core/src/leann/cli.py Outdated
Comment thread packages/leann-core/src/leann/cli.py Outdated
@andylizf

Copy link
Copy Markdown
Collaborator

Also, in the future, we may want to refactor our document_rag to make it the default implementation of leann cli, which will reduce a lot of redundant code.

@yichuan-w

Copy link
Copy Markdown
Collaborator

@gabriel-dehan

Copy link
Copy Markdown
Contributor Author

We recently came across astchunk, the pkg you are using and its accompanying paper, too. Have you had a chance to evaluate it or compare it against naive chunking? I’m not entirely convinced about the extent of its performance gains, but it could be worth discussing further.

I've been asking myself the same thing. I've got no idea on how to measure that properly so I used Claude Code to generate a quick and dirty evaluation framework on small and big codebases, it does a lot of things and isn't very clean but it clones big repositories like VSCode / Node / Jango and performs complex queries on it amongst other things.

The results are here: gabriel-dehan#2
I am not saying we should trust them 100%, I've skimmed over the evaluation code and it seems coherent overall but there is way too much stuff there and a lot I don't have the time to understand. But I feel it gives an idea, and the results are not that far from what I'd expect (except for the memory and speed difference, I am a bit skeptical about this)

I'll check your other comments and implement the fixes.

@yichuan-w

Copy link
Copy Markdown
Collaborator

Yeah, thanks for the benchmark. I briefly looked at that, and I am confused about the efficiency and memory-saving gain. Maybe I can investigate that later, but at least we can first merge this PR and make it an option, thanks a lot!! Both this PR and the benchmark are great.

Let's fix this PR and merge it first

@gabriel-dehan

gabriel-dehan commented Aug 17, 2025

Copy link
Copy Markdown
Contributor Author

Rebased on main, made the requested changes (for the most part) and installed the pre-commit hook and fixed a bunch of violations. Also commited the uv.lock so the PR seems like a lot more changes than what it actually has 🤭

gabriel-dehan and others added 9 commits August 17, 2025 18:38
This PR introduces intelligent code chunking that preserves semantic boundaries
(functions, classes, methods) for better code understanding in RAG applications.

Key Features:
- AST-aware chunking for Python, Java, C#, TypeScript files
- Graceful fallback to traditional chunking for unsupported languages
- New specialized code RAG application for repositories
- Enhanced CLI with --use-ast-chunking flag
- Comprehensive test suite with integration tests

Technical Implementation:
- New chunking_utils.py module with enhanced chunking logic
- Extended base RAG framework with AST chunking arguments
- Updated document RAG with --enable-code-chunking flag
- CLI integration with proper error handling and fallback

Benefits:
- Better semantic understanding of code structure
- Improved search quality for code-related queries
- Maintains backward compatibility with existing workflows
- Supports mixed content (code + documentation) seamlessly

Dependencies:
- Added astchunk and tree-sitter parsers to pyproject.toml
- All dependencies are optional - fallback works without them

Testing:
- Comprehensive test suite in test_astchunk_integration.py
- Integration tests with document RAG
- Error handling and edge case coverage

Documentation:
- Updated README.md with AST chunking highlights
- Added ASTCHUNK_INTEGRATION.md with complete guide
- Updated features.md with new capabilities
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@gabriel-dehan
gabriel-dehan force-pushed the feature/integrate-astchunk branch from 9b316c2 to cc41033 Compare August 17, 2025 16:43
@yichuan-w
yichuan-w requested a review from andylizf August 17, 2025 20:04
@yichuan-w

Copy link
Copy Markdown
Collaborator

I guess still some lint error? @gabriel-dehan and thanks for fixing!

@gabriel-dehan

Copy link
Copy Markdown
Contributor Author

Yeah I'll try to look at it tonight!

- Updated packages to v0.3.2
- Added astchunk dependency and tree-sitter dependencies
- Resolved uv.lock conflicts by regenerating
@yichuan-w

Copy link
Copy Markdown
Collaborator

LGTM, let's wait for CI and we will add you to our ack!

@yichuan-w

Copy link
Copy Markdown
Collaborator

Looks good, we can merge first and make it default false, if it works well, we can make it default true!

@yichuan-w
yichuan-w merged commit 13bb561 into StarTrail-org:main Aug 20, 2025
22 checks passed
yichuan-w added a commit that referenced this pull request Aug 22, 2025
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.

4 participants