Skip to content

Commit 317a692

Browse files
committed
feat(03-02): replace exception handlers in embeddings and chunker
- Catch openai.APIError in embedding_dimension property - Use generic message for UnexpectedEmbeddingError - Catch VLM APITimeoutError and APIError separately in chunker - Use logger.exception for unexpected errors in contextualize_chunks
1 parent 04c4a8c commit 317a692

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

openrag/components/indexer/chunker/chunker.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,20 @@ async def _generate_context(
6868
]
6969
output = await self.context_generator.ainvoke(messages)
7070
return output.content
71+
7172
except openai.APITimeoutError:
72-
logger.warning(
73-
f"OpenAI API timeout contextualizing chunk after {CONTEXTUALIZATION_TIMEOUT}s",
74-
filename=filename,
75-
)
73+
# VLM timeout - graceful degradation
74+
logger.warning("VLM context generation timeout", timeout=CONTEXTUALIZATION_TIMEOUT)
75+
return ""
76+
77+
except openai.APIError as e:
78+
# Other VLM API errors - log but don't fail chunking
79+
logger.error("VLM context generation failed", error=str(e))
7680
return ""
81+
7782
except Exception as e:
78-
logger.warning(
79-
"Error contextualizing chunk of document",
80-
filename=filename,
81-
error=str(e),
82-
)
83+
# Unexpected errors - log but still gracefully degrade
84+
logger.exception("Unexpected error during context generation")
8385
return ""
8486

8587
async def contextualize_chunks(
@@ -131,7 +133,7 @@ async def contextualize_chunks(
131133
]
132134

133135
except Exception as e:
134-
logger.warning(f"Error contextualizing chunks from `{filename}`: {e}")
136+
logger.exception("Error contextualizing chunks", filename=filename)
135137
return chunks
136138

137139

openrag/components/indexer/embeddings/openai.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,18 @@ def embedding_dimension(self) -> int:
2323
# Test call to get embedding dimension
2424
output = self.embed_documents([Document(page_content="test")])
2525
return len(output[0])
26-
except Exception:
27-
raise
26+
27+
except openai.APIError as e:
28+
logger.error("Failed to get embedding dimension", error=str(e))
29+
raise EmbeddingAPIError(f"API error: {e}", model_name=self.embedding_model)
30+
31+
except (IndexError, AttributeError) as e:
32+
logger.error("Invalid embedding response format", error=str(e))
33+
raise EmbeddingResponseError("Unexpected response format", error=str(e))
34+
35+
except Exception as e:
36+
logger.exception("Unexpected error getting embedding dimension")
37+
raise UnexpectedEmbeddingError("An unexpected error occurred")
2838

2939
def embed_documents(self, texts: list[str | Document]) -> list[list[float]]:
3040
"""
@@ -62,7 +72,7 @@ def embed_documents(self, texts: list[str | Document]) -> list[list[float]]:
6272
except Exception as e:
6373
logger.exception("Unexpected error while embedding documents", error=str(e))
6474
raise UnexpectedEmbeddingError(
65-
f"Failed to embed documents: {e!s}",
75+
"An unexpected error occurred during document embedding",
6676
model_name=self.embedding_model,
6777
base_url=self.base_url,
6878
error=str(e),

0 commit comments

Comments
 (0)