Context
The backend FastAPI endpoints (/bias, /process, /chat) currently process user requests without full validation, and errors thrown by the NLP pipelines or vector database modules are not consistently handled. While Pydantic models exist (URlRequest and ChatQuery), they allow any string, which can lead to invalid URLs or empty chat messages. Additionally, exceptions from functions like run_scraper_pipeline, check_bias, run_langgraph_workflow, search_pinecone, or ask_llm can propagate unhandled, returning inconsistent 500 errors to the frontend.
Problem:
- Input data is not fully validated (invalid URLs, empty messages).
- Endpoint errors are inconsistent and sometimes unhandled.
- Frontend and API consumers cannot reliably parse error responses, making debugging harder.
Proposed Solution:
-
Strong Input Validation:
- Use
pydantic.HttpUrl for URL fields in URlRequest.
- Ensure non-empty messages for
/chat using Pydantic validators.
- Return structured 400 errors for invalid inputs.
-
Centralized Error Handling Middleware:
- Implement a global FastAPI exception handler using
@app.exception_handler.
- Catch both
HTTPException and unhandled exceptions.
- Return a consistent JSON error format, e.g.:
{
"error": "<error_message>",
"code": "<error_type>"
}
- Optionally log exceptions with trace IDs for easier debugging.
Impact:
- Improves backend reliability and maintainability.
- Provides consistent error responses to the frontend.
- Prevents invalid input from breaking NLP and LangGraph pipelines.
- Enhances developer and contributor experience by reducing unexpected server errors.
What Needs to Be Done
- Strong Input Validation
- Centralized Error Handling Middleware
Resources
Getting Started
Context
The backend FastAPI endpoints (
/bias,/process,/chat) currently process user requests without full validation, and errors thrown by the NLP pipelines or vector database modules are not consistently handled. While Pydantic models exist (URlRequestandChatQuery), they allow any string, which can lead to invalid URLs or empty chat messages. Additionally, exceptions from functions likerun_scraper_pipeline,check_bias,run_langgraph_workflow,search_pinecone, orask_llmcan propagate unhandled, returning inconsistent 500 errors to the frontend.Problem:
Proposed Solution:
Strong Input Validation:
pydantic.HttpUrlfor URL fields inURlRequest./chatusing Pydantic validators.Centralized Error Handling Middleware:
@app.exception_handler.HTTPExceptionand unhandled exceptions.{ "error": "<error_message>", "code": "<error_type>" }Impact:
What Needs to Be Done
Resources
Getting Started