@@ -340,7 +340,8 @@ async def save_conversation(
340340
341341 item = {
342342 "id" : conversation_id ,
343- "user_id" : user_id , # Partition key field (matches container definition /user_id)
343+ "userId" : user_id , # Partition key field (matches container definition /userId)
344+ "user_id" : user_id , # Keep for backward compatibility
344345 "messages" : messages ,
345346 "brief" : brief .model_dump () if brief else None ,
346347 "metadata" : metadata or {},
@@ -373,12 +374,16 @@ async def save_generated_content(
373374 conversation = await self .get_conversation (conversation_id , user_id )
374375
375376 if conversation :
377+ # Ensure userId is set (for partition key) - migrate old documents
378+ if not conversation .get ("userId" ):
379+ conversation ["userId" ] = conversation .get ("user_id" ) or user_id
376380 conversation ["generated_content" ] = generated_content
377381 conversation ["updated_at" ] = datetime .now (timezone .utc ).isoformat ()
378382 else :
379383 conversation = {
380384 "id" : conversation_id ,
381- "user_id" : user_id , # Partition key field
385+ "userId" : user_id , # Partition key field
386+ "user_id" : user_id , # Keep for backward compatibility
382387 "messages" : [],
383388 "generated_content" : generated_content ,
384389 "updated_at" : datetime .now (timezone .utc ).isoformat ()
@@ -409,12 +414,16 @@ async def add_message_to_conversation(
409414 conversation = await self .get_conversation (conversation_id , user_id )
410415
411416 if conversation :
417+ # Ensure userId is set (for partition key) - migrate old documents
418+ if not conversation .get ("userId" ):
419+ conversation ["userId" ] = conversation .get ("user_id" ) or user_id
412420 conversation ["messages" ].append (message )
413421 conversation ["updated_at" ] = datetime .now (timezone .utc ).isoformat ()
414422 else :
415423 conversation = {
416424 "id" : conversation_id ,
417- "user_id" : user_id , # Partition key field
425+ "userId" : user_id , # Partition key field
426+ "user_id" : user_id , # Keep for backward compatibility
418427 "messages" : [message ],
419428 "updated_at" : datetime .now (timezone .utc ).isoformat ()
420429 }
@@ -443,9 +452,10 @@ async def get_user_conversations(
443452 # This handles legacy data before "anonymous" was used as the default
444453 if user_id == "anonymous" :
445454 query = """
446- SELECT TOP @limit c.id, c.user_id, c.updated_at, c.messages, c.brief, c.metadata
455+ SELECT TOP @limit c.id, c.userId, c. user_id, c.updated_at, c.messages, c.brief, c.metadata
447456 FROM c
448- WHERE c.user_id = @user_id
457+ WHERE c.userId = @user_id
458+ OR c.user_id = @user_id
449459 OR c.user_id = ""
450460 OR c.user_id = null
451461 OR NOT IS_DEFINED(c.user_id)
@@ -457,9 +467,9 @@ async def get_user_conversations(
457467 ]
458468 else :
459469 query = """
460- SELECT TOP @limit c.id, c.user_id, c.updated_at, c.messages, c.brief, c.metadata
470+ SELECT TOP @limit c.id, c.userId, c. user_id, c.updated_at, c.messages, c.brief, c.metadata
461471 FROM c
462- WHERE c.user_id = @user_id
472+ WHERE c.userId = @user_id OR c. user_id = @user_id
463473 ORDER BY c.updated_at DESC
464474 """
465475 params = [
@@ -523,13 +533,21 @@ async def delete_conversation(
523533 """
524534 await self .initialize ()
525535
526- # Delete the conversation using user_id as partition key
536+ # Get the conversation to find its partition key value
537+ conversation = await self .get_conversation (conversation_id , user_id )
538+ if not conversation :
539+ # Already doesn't exist, consider it deleted
540+ return True
541+
542+ # Use userId (camelCase) as partition key, fallback to user_id for old documents
543+ partition_key = conversation .get ("userId" ) or conversation .get ("user_id" ) or user_id
544+
527545 try :
528546 await self ._conversations_container .delete_item (
529547 item = conversation_id ,
530- partition_key = user_id
548+ partition_key = partition_key
531549 )
532- logger .info (f"Deleted conversation { conversation_id } " )
550+ logger .info (f"Deleted conversation { conversation_id } with partition key: { partition_key } " )
533551 return True
534552 except Exception as e :
535553 logger .warning (f"Failed to delete conversation { conversation_id } : { e } " )
@@ -560,6 +578,9 @@ async def rename_conversation(
560578
561579 conversation ["metadata" ] = conversation .get ("metadata" , {})
562580 conversation ["metadata" ]["custom_title" ] = new_title
581+ # Ensure userId is set (for partition key) - migrate old documents
582+ if not conversation .get ("userId" ):
583+ conversation ["userId" ] = conversation .get ("user_id" ) or user_id
563584 # Don't update updated_at - renaming shouldn't change sort order
564585
565586 result = await self ._conversations_container .upsert_item (conversation )
0 commit comments