Skip to content

feat: include job_id, run_id, and task_key in adapter_response - #1376

Closed
4444J99 wants to merge 5 commits into
databricks:mainfrom
organvm:feat/include-job-context-in-adapter-response
Closed

feat: include job_id, run_id, and task_key in adapter_response#1376
4444J99 wants to merge 5 commits into
databricks:mainfrom
organvm:feat/include-job-context-in-adapter-response

Conversation

@4444J99

@4444J99 4444J99 commented Mar 28, 2026

Copy link
Copy Markdown

Resolves #722

Description

When dbt runs inside a Databricks workflow task, the Databricks runtime sets DATABRICKS_JOB_ID, DATABRICKS_RUN_ID, and DATABRICKS_TASK_KEY as environment variables. This PR reads those variables and includes them in the adapter_response, making them available in run_results.json so that downstream tooling can correlate a dbt run with its originating Databricks Job execution.

Implementation:

  • Introduces DatabricksAdapterResponse, a subclass of AdapterResponse that adds three optional fields: job_id, run_id, and task_key
  • A helper function _get_job_run_context() reads the environment variables once and returns a dict with the values (or None when running outside a Databricks Job)
  • CursorWrapper.get_response() and the fallback path in DatabricksConnectionManager.get_response() both return the new response type
  • When running outside a Databricks Job context, all three fields are None — fully backward-compatible

Example output in run_results.json (when running inside a Databricks Job):

{
  "adapter_response": {
    "_message": "OK",
    "query_id": "01f0-...",
    "job_id": "123456",
    "run_id": "789012",
    "task_key": "dbt_run"
  }
}

Checklist

  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • I have updated the CHANGELOG.md and added information about my change to the "dbt-databricks next" section.

When dbt runs inside a Databricks workflow task, the runtime exposes
DATABRICKS_JOB_ID, DATABRICKS_RUN_ID, and DATABRICKS_TASK_KEY as
environment variables. This change reads those variables and surfaces
them in the adapter_response via a new DatabricksAdapterResponse
subclass, making them available in run_results.json for downstream
tooling to correlate dbt runs with their originating Databricks Job
executions.

Outside of a Databricks Job context the fields are None and the
response remains fully backward-compatible.

Resolves databricks#722
@4444J99

4444J99 commented Apr 21, 2026

Copy link
Copy Markdown
Author

Hi team — checking in on this PR. It adds job_id, run_id, and task_key to the adapter response when dbt runs inside a Databricks workflow task, addressing #722. The implementation is fully backward-compatible (all fields are None when running outside a Databricks Job context). Happy to rebase or address any feedback.

@sd-db sd-db self-assigned this Apr 26, 2026
@github-actions

github-actions Bot commented Apr 26, 2026

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  dbt/adapters/databricks
  connections.py 556
  handle.py
Project Total  

This report was generated by python-coverage-comment-action

@sd-db sd-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes look good, thx for the contribution. @4444J99 can you you look to fix the minor comment issue. Should be ready to merge post that.

Comment thread dbt/adapters/databricks/handle.py Outdated
sd-db added 2 commits May 8, 2026 12:38
Removes verbose explanation that duplicates content already documented
in _get_job_run_context's docstring.
Per review feedback from the Databricks Jobs team (Gabor Ratky, Anton
Forsberg): rename the run_id field to task_run_id for consistency with
the canonical TaskRunContext proto, system.lakeflow.job_task_run_timeline
column semantics, and the original feature request in databricks#722. The source
env var DATABRICKS_RUN_ID is set by the runtime and remains unchanged.
@sd-db

sd-db commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Tested this end-to-end by submitting a real dbt_task run with a macro that dumps env_var() for these names — DATABRICKS_JOB_ID, DATABRICKS_RUN_ID, and DATABRICKS_TASK_KEY all came back <UNSET>, so the new fields will always be None.

The dbt task type sets DBT_* env vars instead and exposes job/run identity via DBT_DATABRICKS_HTTP_SESSION_HEADERS (a JSON string containing X-Databricks-Dbsql-Job-Id, X-Databricks-Dbsql-Run-Id, and a base64'd job_run_id). task_key isn't passed to the subprocess at all.

I think the PR requires changes in its current state, also I had done a minor renaming of run_id --> task_run_id for uniformity with system tables etc.

@sd-db
sd-db self-requested a review May 11, 2026 09:31
@sd-db

sd-db commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Closing for #1451, where I have made the fixes. Thx for the contribution. I will followup and look to have this merged in the next few releases.

@sd-db sd-db closed this May 11, 2026
sd-db added a commit that referenced this pull request May 20, 2026
…r_response (#1451)

### Summary
Exposes `job_id`, `job_run_id`, and `task_run_id` on `adapter_response`
for dbt runs executed by a Databricks Jobs `dbt_task`, so users can join
`run_results.json` to `system.lakeflow.{jobs, job_run_timeline,
job_task_run_timeline}`.

Source: parses `DBT_DATABRICKS_HTTP_SESSION_HEADERS`, the env var the
Jobs runtime sets on the dbt CLI subprocess. Fields are `None` outside a
`dbt_task` (local CLI, notebook task, etc.) — fully backward-compatible.

Resolves #722
Refs #1376
### Verification

- **Unit tests** cover env-var absent, empty, valid, missing
query-source, malformed JSON/base64. Full unit suite: 771 passed.
- Ran **dbt_task submissions on a live workspace** to verify.
- **Single-task run**: extracted the three IDs from the resulting
run_results.json, then queried system.lakeflow.job_task_run_timeline and
system.lakeflow.job_run_timeline for that job_id. The task table's
run_id matched our task_run_id, its job_run_id matched ours, its job_id
matched. The parent table's run_id matched our job_run_id. Names line up
with the join columns.
- **Multi-task run**: three sibling dbt_tasks in one submission, no
depends_on. Two ran `dbt run-operation dump_ids` (a macro that just
log()s the raw DBT_DATABRICKS_HTTP_SESSION_HEADERS); the third ran `dbt
run`. All three tasks reported the same job_run_id and three different
task_run_ids, each matching that task's own run_id in the jobs API
response as well as the values in system tables.
- **Local dbt run**: outside a dbt_task writes a run_results.json with
the three fields stripped (dbt-core drops None dataclass fields) — no
log pollution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include job_id, run_id and task_id in adapter_response in the results on-run-end context variable

2 participants