1818 - Azure AI Search / RAG integration with enhanced citation display (Azure OpenAI only)
1919"""
2020
21- from typing import List , Union , Generator , Iterator , Optional , Dict , Any , AsyncIterator
21+ from typing import (
22+ List ,
23+ Union ,
24+ Generator ,
25+ Iterator ,
26+ Optional ,
27+ Dict ,
28+ Any ,
29+ AsyncIterator ,
30+ Set ,
31+ )
2232from urllib .parse import urlparse
2333from fastapi .responses import StreamingResponse
2434from pydantic import BaseModel , Field , GetCoreSchemaHandler
3040import logging
3141import base64
3242import hashlib
43+ import re
3344from pydantic_core import core_schema
3445
3546
@@ -609,9 +620,10 @@ async def stream_processor_with_citations(
609620 "content"
610621 ]
611622 except json .JSONDecodeError :
623+ # Malformed or incomplete JSON is expected in streamed chunks; safely skip.
612624 pass
613- except Exception :
614- pass
625+ except Exception as e :
626+ log . debug ( f"Exception while processing chunk: { e } " )
615627
616628 # Look for citations in any part of the response
617629 if "citations" in chunk_str .lower () and not citations_data :
@@ -768,7 +780,7 @@ async def stream_processor_with_citations(
768780 # Suppress close-time errors (e.g., SSL shutdown timeouts)
769781 pass
770782
771- def _extract_referenced_citations (self , content : str ) -> set :
783+ def _extract_referenced_citations (self , content : str ) -> Set [ int ] :
772784 """
773785 Extract citation references (e.g., [doc1], [doc2]) from the content.
774786
@@ -778,8 +790,6 @@ def _extract_referenced_citations(self, content: str) -> set:
778790 Returns:
779791 Set of citation indices that are referenced (e.g., {1, 2, 7, 8, 9})
780792 """
781- import re
782-
783793 # Find all [docN] references in the content
784794 pattern = r"\[doc(\d+)\]"
785795 matches = re .findall (pattern , content )
@@ -861,7 +871,7 @@ def _format_citation_section(
861871 citation_info .append (f"🔗 **URL:** { url } " )
862872
863873 # Show chunk_id if available and not empty
864- if chunk_id and str (chunk_id ).strip ():
874+ if chunk_id is not None and str (chunk_id ).strip ():
865875 citation_info .append (f"📄 **Chunk ID:** { chunk_id } " )
866876
867877 # Add full content if available
0 commit comments