Commit d18e039
authored
Honor AI_AGENT and pass raw values through (#1454)
## Why
The Python SDK detects AI coding agents and surfaces them as
`agent/<name>` in the User-Agent. Today the generic fallback (when no
proprietary env var fires) only honors the agents.md `AGENT=<name>`
standard. Vercel's `@vercel/detect-agent` library uses a parallel
`AI_AGENT=<name>` convention that tools in the Vercel ecosystem set
instead; we currently miss those.
Separately, the existing fallback coerces any unrecognized value to the
literal string `"unknown"`. That buries useful signal: a tool setting
`AI_AGENT=claude-code_2-1-141_agent` ends up as `agent/unknown`,
discarding the very signal (tool name plus version variant) we want to
see. Bucketing arbitrary names is an ETL concern, not the SDK's.
This mirrors the Go SDK change in databricks/databricks-sdk-go#1683.
## Changes
Two behavior changes in `databricks/sdk/useragent.py`:
1. **`AI_AGENT` fallback.** Add `AI_AGENT=<name>` as a secondary
fallback after `AGENT=<name>`. `AGENT` wins when both are set to
non-empty values; empty is treated as unset for both. Explicit product
matchers (e.g. `CLAUDECODE`) still always win over both.
2. **Raw passthrough instead of `"unknown"`.** Drop the known-product
lookup in the fallback. The value is sanitized (disallowed chars become
`-`, satisfying the User-Agent allowlist `[0-9A-Za-z_.+-]+`) and capped
at 64 chars to keep the header bounded. Known products like `cursor` or
`claude-code` pass through unchanged because they already satisfy the
allowlist.
Same change is landing in `databricks-sdk-java` as a sibling PR.
## Test plan
- [x] `pytest tests/test_user_agent.py` passes (54 tests)
- [x] `ruff format` / `ruff check` clean
- [x] `AI_AGENT=<known product>` returns the product name
- [x] `AI_AGENT=<unrecognized>` returns the raw sanitized value (no
longer `"unknown"`)
- [x] `AGENT` wins over `AI_AGENT` when both are non-empty
- [x] Empty `AGENT` falls through to `AI_AGENT`
- [x] Disallowed chars in `AGENT` / `AI_AGENT` are sanitized to `-`
- [x] Values longer than 64 chars are truncated
- [x] Explicit matcher (e.g. `CLAUDECODE`) still wins over both
fallbacks1 parent caecb8b commit d18e039
3 files changed
Lines changed: 145 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
26 | 31 | | |
27 | 32 | | |
28 | 33 | | |
| |||
133 | 138 | | |
134 | 139 | | |
135 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
136 | 146 | | |
137 | 147 | | |
138 | 148 | | |
| |||
226 | 236 | | |
227 | 237 | | |
228 | 238 | | |
229 | | - | |
| 239 | + | |
| 240 | + | |
230 | 241 | | |
231 | 242 | | |
232 | 243 | | |
| |||
236 | 247 | | |
237 | 248 | | |
238 | 249 | | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
239 | 256 | | |
240 | 257 | | |
241 | 258 | | |
| |||
274 | 291 | | |
275 | 292 | | |
276 | 293 | | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
281 | 297 | | |
282 | | - | |
283 | | - | |
| 298 | + | |
| 299 | + | |
284 | 300 | | |
285 | 301 | | |
286 | 302 | | |
| |||
301 | 317 | | |
302 | 318 | | |
303 | 319 | | |
304 | | - | |
| 320 | + | |
305 | 321 | | |
306 | | - | |
307 | | - | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
308 | 327 | | |
309 | | - | |
| 328 | + | |
310 | 329 | | |
311 | 330 | | |
312 | | - | |
313 | | - | |
314 | | - | |
| 331 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
259 | | - | |
260 | | - | |
261 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
265 | | - | |
| 265 | + | |
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
269 | | - | |
270 | | - | |
271 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
278 | 303 | | |
279 | 304 | | |
280 | 305 | | |
| |||
293 | 318 | | |
294 | 319 | | |
295 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
296 | 401 | | |
297 | 402 | | |
298 | 403 | | |
| |||
0 commit comments