11import logging
2- from typing import Any
2+ from collections .abc import Mapping , Sequence
3+ from typing import Any , cast
34from uuid import UUID
45
56from langfuse import Langfuse
6- from sqlmodel import Session , select
7+ from sqlmodel import Session , col , select
78
89from app .core .cloud .storage import get_cloud_storage
910from app .core .db import engine
@@ -119,7 +120,7 @@ def list_evaluation_runs(
119120 project_id : int ,
120121 limit : int = 50 ,
121122 offset : int = 0 ,
122- ) -> list [EvaluationRun ]:
123+ ) -> Sequence [EvaluationRun ]:
123124 """
124125 List all evaluation runs for an organization and project.
125126
@@ -138,7 +139,7 @@ def list_evaluation_runs(
138139 .where (EvaluationRun .organization_id == organization_id )
139140 .where (EvaluationRun .project_id == project_id )
140141 .where (EvaluationRun .type == EvaluationType .TEXT .value )
141- .order_by (EvaluationRun .inserted_at .desc ())
142+ .order_by (col ( EvaluationRun .inserted_at ) .desc ())
142143 .limit (limit )
143144 .offset (offset )
144145 )
@@ -301,7 +302,7 @@ def get_or_fetch_score(
301302 logger .info (
302303 f"[get_or_fetch_score] Returning existing score | evaluation_id={ eval_run .id } "
303304 )
304- return eval_run .score
305+ return cast ( EvaluationScore , eval_run .score )
305306
306307 logger .info (
307308 f"[get_or_fetch_score] Fetching score from Langfuse | "
@@ -339,7 +340,7 @@ def get_or_fetch_score(
339340 update_evaluation_run (
340341 session = session ,
341342 eval_run = eval_run ,
342- update = EvaluationRunUpdate (score = score ),
343+ update = EvaluationRunUpdate (score = cast ( dict [ str , Any ], score ) ),
343344 )
344345
345346 total_traces = len (score .get ("traces" , []))
@@ -356,7 +357,7 @@ def _upload_score_traces(
356357 session : Session ,
357358 eval_run_id : int ,
358359 project_id : int ,
359- traces : list [ dict [str , Any ]],
360+ traces : Sequence [ Mapping [str , Any ]],
360361) -> str | None :
361362 """Upload per-trace records to S3 for an evaluation run.
362363
@@ -399,7 +400,7 @@ def persist_score_traces(
399400 eval_run_id : int ,
400401 organization_id : int ,
401402 project_id : int ,
402- traces : list [ dict [str , Any ]],
403+ traces : Sequence [ Mapping [str , Any ]],
403404) -> EvaluationRun | None :
404405 """Persist the Q&A trace skeleton to S3 and record the ``score_trace_url``
405406 pointer, WITHOUT touching the ``score`` column (keeps the run score-less
@@ -484,12 +485,13 @@ def save_score(
484485 # IF TRACES DATA IS STORED IN S3 URL THEN HERE WE ARE JUST STORING THE SUMMARY SCORE
485486 # TODO: Evaluate whether this behaviour is needed or completely discard the storing data in db
486487 if score_trace_url :
487- db_score = {"summary_scores" : summary_score }
488- if score .get ("overall" ) is not None :
489- db_score ["overall" ] = score ["overall" ]
488+ db_score : dict [str , Any ] = {"summary_scores" : summary_score }
489+ overall = score .get ("overall" )
490+ if overall is not None :
491+ db_score ["overall" ] = overall
490492 else :
491493 # fallback to store data in db if failed to store in s3
492- db_score = score
494+ db_score = cast ( dict [ str , Any ], score )
493495
494496 update_evaluation_run (
495497 session = session ,
@@ -539,6 +541,8 @@ def group_traces_by_question_id(
539541
540542 for trace in traces :
541543 question_id = trace .get ("question_id" )
544+ if question_id is None :
545+ continue
542546 if question_id not in groups :
543547 groups [question_id ] = []
544548 groups [question_id ].append (trace )
0 commit comments