Every interaction with PDF Insight Pro is scoped to a session — a server-side record identified by a UUID that ties together theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Jatin-Mehra119/PDF-Insight-Beta/llms.txt
Use this file to discover all available pages before exploring further.
SentenceTransformer embedding model, the faiss.IndexHNSWFlat vector index, the ChatGroq LLM instance, the list of document chunks with their metadata, and the full conversation history. The SessionManager class in services/session_service.py owns the entire lifecycle: it creates sessions on upload, retrieves and reconstructs them on each chat turn, appends to chat history after every response, and deletes all associated files on removal. A global singleton instance (session_manager) is shared across all route handlers.
Session Lifecycle
Session created — SessionManager.create_session()
Called by
upload_pdf_handler after the PDF has been loaded and chunked. The method:- Generates a fresh
uuid.uuid4()session ID. - Instantiates a
SentenceTransformer(Config.EMBEDDING_MODEL)(BAAI/bge-large-en-v1.5). - Calls
create_embeddings(chunks_with_metadata, embedding_model)to produce the embedding matrix. - Calls
build_faiss_index(embeddings)to produce the in-memoryIndexHNSWFlat. - Calls
create_llm_model(model_name)to instantiate aChatGroqobject. - Assembles the full session dict and writes it to both the in-memory cache (
self.active_sessions) and to disk viasave_session_to_file().
Session retrieved — SessionManager.get_session()
Called on every
POST /chat request. The lookup follows a two-stage strategy:Stage 1 — In-memory cache hit. If session_id is already in self.active_sessions, the cached dict is returned immediately. Two consistency checks are performed before returning:- If the cached
llmisNoneor itsmodel_namediffers from the requested model, a newChatGroqinstance is created in-place. - If the
model(embedding model) isNone, a freshSentenceTransformeris instantiated. - If
indexisNonebutchunksis present, the FAISS index is rebuilt from scratch viacreate_embeddings()+build_faiss_index().
load_session_from_file(session_id) reads the pickle from uploads/{session_id}_session.pkl. The loaded data contains only file_path, file_name, chunks, and chat_history — the non-serialisable objects are absent. reconstruct_session_objects() re-creates the SentenceTransformer, rebuilds the FAISS index from the stored chunks, and instantiates a new ChatGroq. The fully-reconstructed dict is cached back into self.active_sessions before being returned.Chat entry appended — add_chat_entry()
After
rag_service.generate_response() returns, the chat handler calls session_manager.add_chat_entry(session_id, user_message, assistant_message). The method appends a {"user": ..., "assistant": ...} dict to the session’s chat_history list and immediately re-serialises the session to disk via save_session() → save_session_to_file().Session removed — remove_session()
Triggered by
POST /remove-pdf. The method deletes the session from self.active_sessions and delegates disk cleanup to cleanup_session_files(session_id) in utils/session_utils.py. The cleanup function reads the pickle to retrieve file_path, deletes the PDF from uploads/, and then removes the pickle file itself.Session Data Structure
A fully-populated session dict contains the following keys:| Key | Type | Description |
|---|---|---|
file_path | str | Absolute path to the uploaded PDF in uploads/, e.g. uploads/{session_id}_{filename}.pdf |
file_name | str | Original filename as submitted by the client |
chunks | List[Dict] | List of {"text": str, "metadata": dict} dicts produced by chunk_text() |
model | SentenceTransformer | Embedding model instance (BAAI/bge-large-en-v1.5) — not serialised to disk |
index | faiss.IndexHNSWFlat | In-memory FAISS HNSW index — not serialised to disk |
llm | ChatGroq | LangChain-Groq chat model instance — not serialised to disk |
chat_history | List[Dict] | Ordered list of {"user": str, "assistant": str} exchange records |
Only
file_path, file_name, chunks, and chat_history are written to the pickle. The three non-serialisable objects (model, index, llm) are always reconstructed at load time — this is intentional and handled by reconstruct_session_objects() in utils/session_utils.py.Persistence
PDF Insight Pro uses a hybrid in-memory + file strategy for session persistence. The in-memory cache (SessionManager.active_sessions) is a plain Python dict keyed by session UUID. It provides O(1) lookups and avoids repeated deserialisaton on every request within the same process lifetime. The on-disk representation is a Python pickle file written to Config.UPLOAD_DIR (uploads/) using the path template {session_id}_session.pkl.
uploads/ directory:
Validation
Two functions guard against sessions with missing components before they reach the agent.validate_session_data(session_data) in utils/session_utils.py checks that all four required runtime keys are present:
SessionManager.validate_session(session_id) is the higher-level wrapper called by chat_handler after get_session() returns. It calls get_session() internally to ensure the in-memory copy is up-to-date, then delegates to validate_session_data() and returns the (is_valid, missing_keys) tuple. If is_valid is False, the chat handler raises an HTTP 500 with ErrorMessages.SESSION_INCOMPLETE.