Hybrid RAG: Structured and Unstructured Context Together
When part of what a user needs lives in documents and part lives in the current session, retrieval has to cover both — not pick one.
Problem
Standard RAG assumes the answer lives somewhere in a document corpus. That assumption breaks down the moment part of the needed context is structured, session-specific data that was never going to be embedded as a document in the first place.
Constraints
- The system needed to stay grounded in a stable, persistent knowledge base for facts that don't change session to session
- It also needed access to session-specific structured data that has no reason to exist as an embedded document
- Authentication and access control had to hold across both retrieval paths, not just the document layer
Architecture
Two grounding sources feed the same system: persistent document embeddings in a vector database for stable knowledge, and session-based structured data supplied directly for the current interaction. Both are combined at query time rather than treating one as primary and the other as a bolt-on, with FastAPI and LangChain orchestrating the retrieval and generation flow, and OpenAI models handling generation. Docker packages the whole system for deployment.
Trade-offs
- Maintaining two distinct context sources costs more engineering complexity than a single-source RAG pipeline — justified here because a single-source approach genuinely couldn't answer the full range of real questions
- Treating session-based structured data as a first-class context source (not an afterthought) meant designing the retrieval layer around both sources from the start, rather than retrofitting structured data support onto a document-only system later
Lessons Learned
- The instinct to reach for 'more embeddings' when a RAG system underperforms is often wrong when the missing context isn't in any document to begin with
- Designing for two context sources from day one is simpler than bolting a second one onto a system built assuming only one
Technologies
When to reach for this pattern
Worth the added complexity specifically when a domain has both stable, document-shaped knowledge and genuinely session-specific structured context that a user needs answered together. If everything relevant actually lives in documents, a single-source RAG pipeline is simpler and just as effective.