Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: pass effective jwt token to API calls (#1088)

Lucas Oliveira685ecd31a5c9
diff --git a/sdks/python/tests/test_integration.py b/sdks/python/tests/test_integration.py
index 2df299491..df98a034c 100644
--- a/sdks/python/tests/test_integration.py
+++ b/sdks/python/tests/test_integration.py
@@ -127,6 +127,22 @@ class TestSettings:
assert updated_settings.knowledge.chunk_size == current_chunk_size
+class TestModels:
+ """Test models endpoint."""
+
+ @pytest.mark.asyncio
+ async def test_list_models(self, client):
+ """Test listing models for a provider."""
+ # This tests both the API key auth and the minted JWT
+ # since models_service often needs credentials/JWT
+ models = await client.models.list("openai")
+
+ assert models.language_models is not None
+ assert isinstance(models.language_models, list)
+ assert models.embedding_models is not None
+ assert isinstance(models.embedding_models, list)
+
+
class TestKnowledgeFilters:
"""Test knowledge filter operations."""
@@ -406,3 +422,26 @@ class TestChat:
result = await client.chat.delete(response.chat_id)
assert result is True
+
+ @pytest.mark.asyncio
+ async def test_chat_with_sources(self, client, test_file: Path):
+ """Test chat uses embedded knowledge (RAG), not just pure LLM."""
+ # 1. Ingest document
+ result = await client.documents.ingest(file_path=str(test_file))
+ if result.status == "failed" or result.successful_files == 0:
+ pytest.skip("Document ingestion failed — cannot test RAG sources")
+
+ # 2. Wait for indexing
+ import asyncio
+ await asyncio.sleep(3)
+
+ # 3. Chat about document content
+ response = await client.chat.create(
+ message="What is the color of the dancing animals mentioned in my documents?"
+ )
+
+ # 4. Verify sources — proves RAG retrieval worked
+ assert response.sources is not None
+ assert len(response.sources) > 0
+ source_filenames = [s.filename for s in response.sources]
+ assert any(test_file.name in name for name in source_filenames)
diff --git a/src/agent.py b/src/agent.py
index 1fa2f4b76..172bf23df 100644
--- a/src/agent.py
+++ b/src/agent.py
@@ -624,8 +624,25 @@ async def async_langflow_chat(
message_count=len(conversation_state["messages"]),
)
+ # Extract sources from retrieval tool calls in the response
+ sources = []
+ if hasattr(response_obj, "output") and response_obj.output:
+ for output_item in response_obj.output:
+ item_type = getattr(output_item, "type", None)
+ if item_type in ("tool_call", "retrieval_call"):
+ for result in getattr(output_item, "results", None) or []:
+ rd = result.model_dump() if hasattr(result, "model_dump") else (result if isinstance(result, dict) else {})
+ if "text" in rd:
+ sources.append({
+ "filename": rd.get("filename", ""),
+ "text": rd.get("text", ""),
+ "score": rd.get("score", 0),
+ "page": rd.get("page"),
+ "mimetype": rd.get("mimetype"),
+ })
+
if not store_conversation:
- return response_text, response_id
+ return response_text, response_id, sources
# Store the conversation thread with its response_id
if response_id:
@@ -661,7 +678,7 @@ async def async_langflow_chat(
else:
logger.warning("No response_id received from langflow, conversation not stored")
- return response_text, response_id
+ return response_text, response_id, sources
# Async langflow function with conversation storage (streaming)
diff --git a/src/api/v1/chat.py b/src/api/v1/chat.py
index ebb191417..b87507afe 100644
--- a/src/api/v1/chat.py
+++ b/src/api/v1/chat.py
@@ -28,6 +28,21 @@ class ChatV1Body(BaseModel):
filter_id: Optional[str] = None
+def _extract_sources(item: dict) -> list[dict]:
+ """Extract sources from a retrieval tool call item."""
+ sources = []
+ for result in item.get("results", []):
+ if isinstance(result, dict) and "text" in result:
+ sources.append({
+ "filename": result.get("filename", ""),
+ "text": result.get("text", ""),
+ "score": result.get("score", 0),
+ "page": result.get("page"),
+ "mimetype": result.get("mimetype"),
+ })
+ return sources
+
+
async def _transform_stream_to_sse(raw_stream, chat_id_container: dict):
"""Transform raw Langflow streaming format to clean SSE events for v1 API."""
full_text = ""
@@ -64,6 +79,13 @@ async def _transform_stream_to_sse(raw_stream, chat_id_container: dict):
full_text += delta_text
yield f"data: {json.dumps({'type': 'content', 'delta': delta_text})}\n\n"
+ # Emit sources from retrieval tool calls
+ item = chunk_data.get("item", {})
+ if item.get("type") in ("retrieval_call", "tool_call") and item.get("results"):
+ sources = _extract_sources(item)
+ if sources:
+ yield f"data: {json.dumps({'type': 'sources', 'sources': sources})}\n\n"
+
if not chat_id:
chat_id = chunk_data.get("id") or chunk_data.get("response_id")
diff --git a/src/dependencies.py b/src/dependencies.py
index 8429fdf24..37bcdbf30 100644
--- a/src/dependencies.py
+++ b/src/dependencies.py
@@ -162,6 +162,7 @@ def get_optional_user(
async def get_api_key_user_async(
request: Request,
api_key_service=Depends(get_api_key_service),
+ session_manager=Depends(get_session_manager),
) -> User:
"""
Async dependency: require API key authentication.
@@ -208,9 +209,12 @@ async def get_api_key_user_async(
provider="api_key",
)
- # API Key users don't typically have a JWT for OpenSearch OIDC,
- # but we can try to get an effective one if needed
- user_with_token = dataclasses.replace(user, jwt_token=None)
+ # Register the API key user so get_effective_jwt_token can find them
+ if user.user_id not in session_manager.users:
+ session_manager.users[user.user_id] = user
+
+ effective_token = session_manager.get_effective_jwt_token(user.user_id, None)
+ user_with_token = dataclasses.replace(user, jwt_token=effective_token)
request.state.user = user_with_token
request.state.api_key_id = user_info["key_id"]
diff --git a/src/services/chat_service.py b/src/services/chat_service.py
index 55d4a98bd..d2bfa8b8f 100644
--- a/src/services/chat_service.py
+++ b/src/services/chat_service.py
@@ -156,7 +156,7 @@ class ChatService:
else:
from agent import async_langflow_chat
- response_text, response_id = await async_langflow_chat(
+ response_text, response_id, sources = await async_langflow_chat(
langflow_client,
LANGFLOW_CHAT_FLOW_ID,
prompt,
@@ -168,6 +168,8 @@ class ChatService:
response_data = {"response": response_text}
if response_id:
response_data["response_id"] = response_id
+ if sources:
+ response_data["sources"] = sources
return response_data
async def langflow_nudges_chat(
@@ -289,7 +291,7 @@ class ChatService:
from agent import async_langflow_chat
- response_text, response_id = await async_langflow_chat(
+ response_text, response_id, _sources = await async_langflow_chat(
langflow_client,
NUDGES_FLOW_ID,
prompt,
diff --git a/src/session_manager.py b/src/session_manager.py
index f9dee81aa..2de2d9653 100644
--- a/src/session_manager.py
+++ b/src/session_manager.py
@@ -264,26 +264,22 @@ class SessionManager:
"""Get the effective JWT token, creating anonymous JWT if needed in no-auth mode"""
from config.settings import is_no_auth_mode
- logger.debug(
- "get_effective_jwt_token",
- user_id=user_id,
- jwt_token_present=(jwt_token is not None),
- no_auth_mode=is_no_auth_mode(),
- )
+ if jwt_token is not None:
+ return jwt_token
- # In no-auth mode, create anonymous JWT if needed
- if jwt_token is None and (is_no_auth_mode() or user_id in (None, AnonymousUser().user_id)):
+ # No token — create one
+ if is_no_auth_mode() or user_id in (None, AnonymousUser().user_id):
+ # anonymous JWT (cached)
if not hasattr(self, "_anonymous_jwt"):
- # Create anonymous JWT token for OpenSearch OIDC
- logger.debug("Creating anonymous JWT")
self._anonymous_jwt = self._create_anonymous_jwt()
- logger.debug(
- "Anonymous JWT created", jwt_prefix=self._anonymous_jwt[:50]
- )
- jwt_token = self._anonymous_jwt
- logger.debug("Using anonymous JWT")
+ return self._anonymous_jwt
- return jwt_token
+ # Auth mode, real user, no token — mint a JWT for them
+ user = self.get_user(user_id)
+ if user:
+ return self.create_jwt_token(user)
+
+ return None
def _create_anonymous_jwt(self) -> str:
"""Create JWT token for anonymous user in no-auth mode"""