Skip to content

Commit 5fe703f

Browse files
authored
Merge pull request #257 from RedHatInsights/more-crash-on-chat
Fix nonetype crahes in code path
2 parents 4c53192 + 5c5cc53 commit 5fe703f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/tangerine/models/conversation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def get_by_user(cls, user_id):
3737
@classmethod
3838
def upsert(cls, conversation_json):
3939
user_id = conversation_json.get("user")
40+
# Normalize "anonymous" to None early to prevent inconsistencies
41+
if user_id == "anonymous":
42+
user_id = None
4043
session_id = conversation_json.get("sessionId")
4144
assistant_name = conversation_json.get("assistantName")
4245

src/tangerine/resources/assistant.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)