@@ -138,19 +138,21 @@ def _to_bool(value):
138138 @staticmethod
139139 def _anonymize_user_id (user_id ):
140140 """
141- Anonymize user ID using SHA256 hash, unless the user ID is 'unknown' .
141+ Anonymize user ID using SHA256 hash, with special handling for anonymous users .
142142
143143 Args:
144- user_id (str): The user ID to anonymize
144+ user_id (str or None ): The user ID to anonymize
145145
146146 Returns:
147- str: The anonymized user ID or 'unknown' if the input was ' unknown'
147+ str: The anonymized user ID or 'anonymous' for None/ unknown users
148148 """
149- if user_id == "unknown" :
150- return user_id
149+ # Handle None and various "anonymous" indicators
150+ if user_id is None or user_id in ("unknown" , "anonymous" , "" ):
151+ return "anonymous"
151152
152- # Create a hash of the user ID for anonymization
153- return hashlib .sha256 (user_id .encode ("utf-8" )).hexdigest ()[
153+ # Ensure user_id is string and create hash
154+ user_str = str (user_id )
155+ return hashlib .sha256 (user_str .encode ("utf-8" )).hexdigest ()[
154156 :16
155157 ] # Use first 16 chars of hash
156158
@@ -233,7 +235,8 @@ def _extract_request_data(self):
233235
234236 interaction_id = request .json .get ("interactionId" , None )
235237 client = request .json .get ("client" , "unknown" )
236- user = request .json .get ("user" ) # Can be None for anonymous sessions
238+ # Normalize user early to prevent None from causing crashes downstream
239+ user = request .json .get ("user" ) or "anonymous"
237240
238241 # Extract the current message data to preserve all fields
239242 current_message = request .json .get ("currentMessage" , {})
@@ -339,8 +342,8 @@ def _get_conversation_history(self, session_uuid, user_id):
339342 )
340343 return []
341344
342- # Ownership verification: skip if user is None/ anonymous, otherwise verify ownership
343- if user_id and not conversation .is_owned_by (user_id ):
345+ # Ownership verification: skip if user is anonymous, otherwise verify ownership
346+ if user_id != "anonymous" and not conversation .is_owned_by (user_id ):
344347 log .warning (
345348 "AUDIT: Session %s not owned by user %s, starting fresh conversation" ,
346349 session_uuid ,
@@ -692,7 +695,8 @@ def post(self, _id=None):
692695 interaction_id = request .json .get ("interactionId" , None )
693696 client = request .json .get ("client" , "unknown" )
694697 model_name = request .json .get ("model" )
695- user = request .json .get ("user" ) # Can be None for anonymous sessions
698+ # Normalize user early to prevent None from causing crashes downstream
699+ user = request .json .get ("user" ) or "anonymous"
696700 disable_agentic = request .json .get ("disable_agentic" , False )
697701
698702 # AUDIT LOG: Request model parameter
0 commit comments