-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathon_error_hook.py
More file actions
76 lines (58 loc) · 2.22 KB
/
Copy pathon_error_hook.py
File metadata and controls
76 lines (58 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Wire a custom error hook for observability (``nullrun.on_error``).
The hook fires BEFORE every ``NullRunError`` raise with the
structured fields the catalog carries (``error_code``,
``retryable``, ``user_action``, ``docs_url``, ``stage``,
``workflow_id``). Pair it with ``@guarded`` so each hook fires once
on failure, and the script still exits cleanly.
This is the recommended integration point for Sentry / dashboards /
PagerDuty — the catalog fields are stable, machine-readable, and
already cover the "what does this error mean + what should the user
do" surface so you don't have to grep docs to build the mapping.
Run:
pip install "nullrun[openai]" openai
export NULLRUN_API_KEY=nr_live_...
export OPENAI_API_KEY=sk-...
python examples/on_error_hook.py
"""
from __future__ import annotations
from _env import load_env
load_env() # populate os.environ from examples/.env (no-op if absent)
import logging
import os
from openai import OpenAI
import nullrun
from nullrun import guarded, init_or_die, protect, shutdown
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("nullrun.example")
@nullrun.on_error
def _to_log(err, ctx):
# `err` is a NullRunError subclass; `ctx` is an ErrorContext.
# Both are documented in nullrun.observability.error_hooks.
log.warning(
"NullRun error",
extra={
"code": err.error_code,
"stage": ctx.stage,
"retryable": err.retryable,
"workflow_id": ctx.workflow_id,
"user_action": err.user_action,
},
)
init_or_die() # reads NULLRUN_API_KEY from os.environ; friendly exit if missing
client = OpenAI()
@guarded
@protect
def answer(prompt: str) -> str:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content or ""
if __name__ == "__main__":
try:
# Force a known error path by setting a budget-capped policy
# on the dashboard first -- otherwise this example succeeds
# and the hook never fires.
print(answer("In one sentence, what does NullRun do?"))
finally:
shutdown()