@@ -772,7 +772,7 @@ async def submit_run_feedback(
772772 body : RunFeedbackRequest ,
773773 current_user : AuthenticatedUser = Depends (get_authenticated_user ),
774774):
775- """Record simple user feedback for a completed run in LangFuse."""
775+ """Record user feedback locally and mirror it to LangFuse when available ."""
776776 session = await get_session (session_id , current_user .user_id )
777777 if session is None :
778778 raise HTTPException (
@@ -795,6 +795,15 @@ async def submit_run_feedback(
795795 status_code = 409 ,
796796 detail = "Feedback can only be submitted for completed runs." ,
797797 )
798+ comment : str | None = None
799+ if body .comment is not None :
800+ trimmed = " " .join (body .comment .strip ().split ())
801+ if not trimmed :
802+ raise HTTPException (status_code = 400 , detail = "Feedback comment cannot be empty." )
803+ if len (trimmed ) > 500 :
804+ raise HTTPException (status_code = 400 , detail = "Feedback comment is too long." )
805+ comment = trimmed
806+
798807 trace_id = run .langfuse_trace_id
799808 observation_id = run .langfuse_observation_id
800809 if not trace_id :
@@ -806,68 +815,48 @@ async def submit_run_feedback(
806815 query = run .query ,
807816 report = run .report ,
808817 )
809- except Exception as exc :
810- raise HTTPException (
811- status_code = 502 , detail = f"Could not link run to LangFuse: { exc } "
812- )
813- if not trace_id :
814- raise HTTPException (
815- status_code = 502 , detail = "Could not link run to LangFuse."
816- )
817- linkage_updated = await update_session_run (
818- run_id = run_id ,
819- user_id = current_user .user_id ,
820- session_id = session_id ,
821- patch = {
818+ except Exception :
819+ # LangFuse is optional. A missing or unavailable observability backend
820+ # must not prevent the product from accepting user feedback.
821+ logger .warning ("Could not create LangFuse feedback anchor" , exc_info = True )
822+ trace_id = None
823+ observation_id = None
824+
825+ submitted_at = datetime .now (UTC ).isoformat ()
826+ patch : dict [str , object ] = {
827+ "feedback_submitted_at" : submitted_at ,
828+ "feedback_helpful" : body .helpful ,
829+ }
830+ if trace_id :
831+ patch .update (
832+ {
822833 "langfuse_trace_id" : trace_id ,
823834 "langfuse_observation_id" : observation_id ,
824- },
825- )
826- if not linkage_updated :
827- raise HTTPException (
828- status_code = 404 ,
829- detail = f"Run '{ run_id } ' not found in session '{ session_id } '." ,
830- )
831-
832- comment : str | None = None
833- if body .comment is not None :
834- trimmed = " " .join (body .comment .strip ().split ())
835- if not trimmed :
836- raise HTTPException (
837- status_code = 400 , detail = "Feedback comment cannot be empty."
838- )
839- if len (trimmed ) > 500 :
840- raise HTTPException (status_code = 400 , detail = "Feedback comment is too long." )
841- comment = trimmed
842-
843- try :
844- submit_user_feedback_score (
845- trace_id = trace_id ,
846- observation_id = observation_id ,
847- helpful = body .helpful ,
848- comment = comment ,
849- )
850- except Exception as exc :
851- raise HTTPException (
852- status_code = 502 , detail = f"Could not submit LangFuse feedback: { exc } "
835+ }
853836 )
854-
855- submitted_at = datetime .now (UTC ).isoformat ()
856837 updated = await update_session_run (
857838 run_id = run_id ,
858839 user_id = current_user .user_id ,
859840 session_id = session_id ,
860- patch = {
861- "feedback_submitted_at" : submitted_at ,
862- "feedback_helpful" : body .helpful ,
863- },
841+ patch = patch ,
864842 )
865843 if not updated :
866844 raise HTTPException (
867845 status_code = 404 ,
868846 detail = f"Run '{ run_id } ' not found in session '{ session_id } '." ,
869847 )
870848
849+ if trace_id :
850+ try :
851+ submit_user_feedback_score (
852+ trace_id = trace_id ,
853+ observation_id = observation_id ,
854+ helpful = body .helpful ,
855+ comment = comment ,
856+ )
857+ except Exception :
858+ logger .warning ("Could not mirror user feedback to LangFuse" , exc_info = True )
859+
871860 return {
872861 "session_id" : session_id ,
873862 "run_id" : run_id ,
0 commit comments