Skip to content

fix(python/flask): catch AIGuardAbortError explicitly (BaseException-safe)#6873

Merged
avara1986 merged 5 commits into
mainfrom
avara1986/deprecate_rasing_dispatch
May 8, 2026
Merged

fix(python/flask): catch AIGuardAbortError explicitly (BaseException-safe)#6873
avara1986 merged 5 commits into
mainfrom
avara1986/deprecate_rasing_dispatch

Conversation

@avara1986
Copy link
Copy Markdown
Member

@avara1986 avara1986 commented May 7, 2026

Motivation

Companion to DataDog/dd-trace-py#17941 (chore(core): deprecate raising_dispatch).

That PR introduces a new `DDBlockException(BaseException)` hierarchy and re-parents `AIGuardAbortError` under it:

```
BaseException
├── Exception ← what `except Exception:` matches
└── DDBlockException ← NEW, BaseException-derived
├── BlockingException
└── AIGuardAbortError ← was Exception-derived, now BaseException-derived
```

The motivation in dd-trace-py is to prevent generic `except Exception:` handlers in user code from accidentally swallowing AI Guard block decisions. As a side effect, the existing pattern in this weblog stops working:

```python
except Exception as e:
if isinstance(e, AIGuardAbortError):
return ..., 403
else:
return ..., 500
```

Because `AIGuardAbortError` is no longer an `Exception` subclass, `except Exception:` does not match it (same way it doesn't match `KeyboardInterrupt`). The block is never entered, the `isinstance` check never runs, the response is never returned, and the request reaches the test as no-status — which is exactly what is breaking `tests/ai_guard/test_ai_guard_sdk.py::Test_Evaluation::test_deny` and `::test_abort` against `python@4.9.0-rc1`.

Changes

`utils/build/docker/python/flask/app.py` — restructure the AI Guard evaluate endpoint:

  • Catch `AIGuardAbortError` explicitly (by class identity, so it works regardless of base class).
  • Keep the broad `except Exception:` for the 500 fallback.

This is more idiomatic Python and forward-compatible with the dd-trace-py exception hierarchy change.

Workflow

  1. ⚠️ Create your PR as draft ⚠️
  2. Work on you PR until the CI passes
  3. Mark it as ready for review
    • Test logic is modified? -> Get a review from RFC owner.
    • Framework is modified, or non obvious usage of it -> get a review from R&P team

🚀 Once your PR is reviewed and the CI green, you can merge it!

🛟 #apm-shared-testing 🛟

Reviewer checklist

  • Anything but `tests/` or `manifests/` is modified ? I have the approval from R&P team
  • A docker base image is modified?
    • the relevant `build-XXX-image` label is present
  • A scenario is added, removed or renamed?

@avara1986 avara1986 added the python Pull requests that update Python code label May 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

CODEOWNERS have been resolved as:

utils/build/docker/python/flask/app.py                                  @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core

@avara1986 avara1986 changed the title [python] fix exception control fix(python/flask): catch AIGuardAbortError explicitly (BaseException-safe) May 7, 2026
@datadog-datadog-prod-us1
Copy link
Copy Markdown

datadog-datadog-prod-us1 Bot commented May 7, 2026

Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b99be7e | Docs | Datadog PR Page | Give us feedback!

@avara1986 avara1986 marked this pull request as ready for review May 7, 2026 13:00
@avara1986 avara1986 requested review from a team as code owners May 7, 2026 13:00
@avara1986 avara1986 requested review from juanjux and wconti27 and removed request for a team May 7, 2026 13:00
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2ddd4d6573

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread utils/build/docker/python/flask/app.py
@avara1986 avara1986 enabled auto-merge (squash) May 8, 2026 13:59
@avara1986 avara1986 merged commit 24c2e96 into main May 8, 2026
309 checks passed
@avara1986 avara1986 deleted the avara1986/deprecate_rasing_dispatch branch May 8, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants