Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document dbtRunner (programmatic invocation) #3118

Merged
merged 11 commits into from
Apr 13, 2023

Conversation

jtcohen6
Copy link
Collaborator

@jtcohen6 jtcohen6 commented Mar 31, 2023

What are you changing in this pull request and why?

I've scaled back my original ambitions from #2674 / #2693, given that we de-prioritized some docs improvements, in favor of bugs/regressions before the v1.5 release (more important!).

I think some human-written docs for now are more than sufficient. I need to add examples for the "advanced" usage patterns, and also add more caveats about what users should/shouldn't expect to remain stable in future versions.

Checklist

Other acceptance criteria

  • remove this example script (completed in #7320)

Adding new pages (delete if not applicable):

  • Add page to website/sidebars.js
  • Provide a unique filename for the new page

@jtcohen6 jtcohen6 added the dbt-core v1.5 Docs impact for the v1.5 release (Apr 2023) label Mar 31, 2023
@netlify
Copy link

netlify bot commented Mar 31, 2023

Deploy Preview for docs-getdbt-com ready!

Name Link
🔨 Latest commit 9ece56d
🔍 Latest deploy log https://app.netlify.com/sites/docs-getdbt-com/deploys/64383da02616f9000871b80a
😎 Deploy Preview https://deploy-preview-3118--docs-getdbt-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added content Improvements or additions to content guides Knowledge best suited for Guides size: medium This change will take up to a week to address labels Mar 31, 2023
@jtcohen6 jtcohen6 marked this pull request as ready for review April 11, 2023 11:56
@jtcohen6 jtcohen6 requested a review from a team as a code owner April 11, 2023 11:56
@jtcohen6 jtcohen6 self-assigned this Apr 12, 2023
title: "Programmatic invocations"
---

In v1.5, dbt-core added support for programmatic invocations. The intent of this entry point is provide **exact parity** with CLI functionality, callable from within a Python script or application.
Copy link
Member

Choose a reason for hiding this comment

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

Something like this?

The intent of this entry point is to expose the existing dbt CLI via a Python entrypoint, so it is callable from a Python script or application.


In v1.5, dbt-core added support for programmatic invocations. The intent of this entry point is provide **exact parity** with CLI functionality, callable from within a Python script or application.

The main entry point is a `dbtRunner` class that wraps around `dbt-core`'s CLI, and allows you to "invoke" CLI commands as Python methods. Each command returns a `dbtRunnerResult` object, which has three attributes:
Copy link
Member

Choose a reason for hiding this comment

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

This should be the opposite? The CLI technically wraps the Click runner that dbtRunner depends on.


### `dbtRunnerResult` and exit codes

There is a 1:1 correspondence between [CLI exit codes](reference/exit-codes) and the `dbtRunnerResult` returned by a programmatic invocation:
Copy link
Member

Choose a reason for hiding this comment

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

Let's put this in a table?

Copy link
Contributor

Choose a reason for hiding this comment

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

@aranke I think the table will be covered by the link.

i.e., [CLI exit codes](reference/exit-codes) will link to this page which has the following table:

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like the idea of tabling it up here as well!

Copy link
Member

Choose a reason for hiding this comment

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

Let's copy the same table here; most people aren't going to click on links.

Copy link
Member

Choose a reason for hiding this comment

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

Wait, it's a different table – here the columns should be: scenario, success, exit_code, result, exception.

2. At least one handled error (e.g. test failure, model build error): `success=False`, exit code `1`, `result` populated (based on the command), `exception` empty.
3. Unhandled error: `success=False`, exit code `2`, `exception` populated with top-level error, `result` empty because the invocation did not gracefully complete and return results.

### Caveats
Copy link
Member

Choose a reason for hiding this comment

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

Should this be SLA or similar?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As in - our SLA (set of guarantees) to end users, in terms of stability of the interface across future versions?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, correct.

@@ -190,4 +190,10 @@ The `EVENT_HISTORY` object has been deprecated and removed in dbt Core v1.4+

Older versions of `dbt-core` made available a full history of events fired during an invocation, in the form of an `EVENT_HISTORY` object.

The Python interface into events is significantly less mature than the structured logging interface. For all use cases, we recommend parsing JSON-formatted logs.
<VersionBlock firstVersion="1.5">
Copy link
Member

Choose a reason for hiding this comment

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

TIL, neat!


### Caveats
| Scenario | CLI Exit Code | `success` | `result` | `exception` |
Copy link
Member

Choose a reason for hiding this comment

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

Nit: numbers should be right-aligned using the |---:| header.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TIL!


- The `results` objects returned by each command are liable to change. Some of these objects are already contracted, in part, because they overlap with [dbt artifacts](dbt-artifacts).
From dbt Core v1.5 onward, we making an ongoing commitment to providing a Python entry point at functional parity with dbt-core's CLI. We reserve the right to change the underlying implementation used to achieve that goal. We expect that the current implementation will unlock real use cases, in the short & medium term, while we work on a set of stable, long-term interfaces that will ultimately replace it.
Copy link
Member

Choose a reason for hiding this comment

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

I like this language, ty.

Pass in params as `**kwargs`, instead of passing in a list of CLI-style strings. At present, dbt will not do any validation or type coercion on your inputs. The subcommand must be specified, in a list, as the first positional argument.
### Overriding parameters

Pass in parameters as Pythonic `**kwargs`, instead of a list of CLI-style strings. At present, dbt will not do any validation or type coercion on your inputs. The subcommand must be specified, in a list, as the first positional argument.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: this is not Pythonic style, Python lets you do this as an escape hatch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Heh, good point. I'm just going to change this to Pass in parameters as keyword arguments

aranke
aranke previously approved these changes Apr 12, 2023
Copy link
Member

@aranke aranke left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, couple nits.

@jtcohen6
Copy link
Collaborator Author

@aranke Thanks for the re-review! Good nits, quickly fixed

Copy link
Contributor

@dbeatty10 dbeatty10 left a comment

Choose a reason for hiding this comment

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

Most Python examples confirmed to work!

Suggestion with code fix for the lone exception.

website/docs/reference/programmatic-invocations.md Outdated Show resolved Hide resolved
website/docs/reference/programmatic-invocations.md Outdated Show resolved Hide resolved
Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>
Copy link
Contributor

@dbeatty10 dbeatty10 left a comment

Choose a reason for hiding this comment

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

Love having these example Python scripts!

@jtcohen6 jtcohen6 merged commit a3b26f3 into current Apr 13, 2023
@jtcohen6 jtcohen6 deleted the jerco/programmatic-invocations-api branch April 13, 2023 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content Improvements or additions to content dbt-core v1.5 Docs impact for the v1.5 release (Apr 2023) guides Knowledge best suited for Guides size: medium This change will take up to a week to address
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants