Skip to content

feat(rag): Add full document content retrieval endpoint #657

Closed

Description

@larryro

Summary

Add a new endpoint to the RAG service that returns the full (or paginated) text content of an indexed document. Currently, the RAG service only exposes chunk-level search — there is no way for agents or workflows to retrieve the complete text of a specific document.

Motivation

Several advanced use cases require reasoning over full documents rather than top-k chunks:

  • Drafting new documents based on existing templates/precedents
  • Comparing multiple documents end-to-end
  • Systematic document review (e.g., due diligence reports)

The existing rag_search tool returns relevance-ranked chunks (top_k=10), which is great for Q&A but insufficient when complete document structure matters.

Proposed API

GET /api/v1/documents/{doc_id}/content

Query Parameters

Parameter Type Default Description
page_start int 1 Start page (1-indexed)
page_end int null End page (inclusive). If null, return from page_start to end
team_id string required Tenant scoping

Response

{
  "document_id": "...",
  "title": "...",
  "content": "...",
  "page_range": { "start": 1, "end": 10 },
  "total_pages": 50,
  "truncated": false,
  "total_chars": 45000
}

Implementation Notes

  • Cap single response at ~50K chars to prevent LLM context overflow
  • Set truncated: true when content exceeds the cap
  • Two possible implementation strategies:
    • Option A: Reassemble from stored chunks in order (fast, but may have gaps at chunk boundaries)
    • Option B: Re-extract from the original file stored in object storage (accurate, supports native page ranges for PDFs)
  • Must respect multi-tenant isolation (team_id filtering)
  • Consider adding a total_pages field to document metadata during ingestion to support pagination

Acceptance Criteria

  • New endpoint returns document content with pagination support
  • Response capped at ~50K chars with truncated flag
  • Multi-tenant isolation enforced
  • Returns 404 for non-existent documents
  • Integration tests covering: normal retrieval, page ranges, truncation, access control
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featpriority: highservice: rag

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions