Feat/improve exception raising for better debugging (#1512)

<!-- .github/pull_request_template.md -->

## Description

When raising exceptions, in some places we lose the stack trace, or in
some cases when we raise custom exceptions - we also lose any metadata
from original exception that could help pinpoint the issue.

This PR updates exception throwing to use [`raise ...
from`](https://docs.python.org/3/reference/simple_stmts.html#raise),
which:
1. preserves stack trace
2. when we throw custom exception, indicates that our exception was
caused by XYZ exception

_this is a scoped down version for upcoming release, rest of the changes
are in #1518_

<!--
Please provide a clear, human-generated description of the changes in
this PR.
DO NOT use AI-generated descriptions. We want to understand your thought
process and reasoning.
-->

## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):

## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->

## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [ ] **I have tested my changes thoroughly before submitting this PR**
- [ ] **This PR contains minimal changes necessary to address the
issue/feature**
- [ ] My code follows the project's coding standards and style
guidelines
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added necessary documentation (if applicable)
- [ ] All new and existing tests pass
- [ ] I have searched existing PRs to ensure this change hasn't been
submitted already
- [ ] I have linked any relevant issues in the description
- [ ] My commits have clear and descriptive messages

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.
This commit is contained in:
Daulet Amirkhanov 2025-10-07 22:19:22 +01:00 committed by GitHub
commit 4b14bd7566
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 5 deletions

View file

@ -6,6 +6,10 @@ on:
permissions:
contents: read
env:
RUNTIME__LOG_LEVEL: ERROR
ENV: 'dev'
jobs:
test-gemini:
name: Run S3 File Storage Test

View file

@ -118,7 +118,7 @@ class GenericAPIAdapter(LLMInterface):
if not (self.fallback_model and self.fallback_api_key and self.fallback_endpoint):
raise ContentPolicyFilterError(
f"The provided input contains content that is not aligned with our content policy: {text_input}"
)
) from error
try:
return await self.aclient.chat.completions.create(
@ -151,4 +151,4 @@ class GenericAPIAdapter(LLMInterface):
else:
raise ContentPolicyFilterError(
f"The provided input contains content that is not aligned with our content policy: {text_input}"
)
) from error

View file

@ -146,11 +146,11 @@ class OpenAIAdapter(LLMInterface):
ContentFilterFinishReasonError,
ContentPolicyViolationError,
InstructorRetryException,
):
) as e:
if not (self.fallback_model and self.fallback_api_key):
raise ContentPolicyFilterError(
f"The provided input contains content that is not aligned with our content policy: {text_input}"
)
) from e
try:
return await self.aclient.chat.completions.create(
@ -183,7 +183,7 @@ class OpenAIAdapter(LLMInterface):
else:
raise ContentPolicyFilterError(
f"The provided input contains content that is not aligned with our content policy: {text_input}"
)
) from error
@observe
@sleep_and_retry_sync()