|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "6fcd2994-0092-4fa3-9bb1-c9c84babadc5", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# How to pass runtime secrets to runnables\n", |
| 9 | + "\n", |
| 10 | + ":::info Requires `langchain-core >= 0.2.22`\n", |
| 11 | + "\n", |
| 12 | + ":::\n", |
| 13 | + "\n", |
| 14 | + "We can pass in secrets to our runnables at runtime using the `RunnableConfig`. Specifically we can pass in secrets with a `__` prefix to the `configurable` field. This will ensure that these secrets aren't traced as part of the invocation:" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 6, |
| 20 | + "id": "92e42e91-c277-49de-aa7a-dfb5c993c817", |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "data": { |
| 25 | + "text/plain": [ |
| 26 | + "7" |
| 27 | + ] |
| 28 | + }, |
| 29 | + "execution_count": 6, |
| 30 | + "metadata": {}, |
| 31 | + "output_type": "execute_result" |
| 32 | + } |
| 33 | + ], |
| 34 | + "source": [ |
| 35 | + "from langchain_core.runnables import RunnableConfig\n", |
| 36 | + "from langchain_core.tools import tool\n", |
| 37 | + "\n", |
| 38 | + "\n", |
| 39 | + "@tool\n", |
| 40 | + "def foo(x: int, config: RunnableConfig) -> int:\n", |
| 41 | + " \"\"\"Sum x and a secret int\"\"\"\n", |
| 42 | + " return x + config[\"configurable\"][\"__top_secret_int\"]\n", |
| 43 | + "\n", |
| 44 | + "\n", |
| 45 | + "foo.invoke({\"x\": 5}, {\"configurable\": {\"__top_secret_int\": 2, \"traced_key\": \"bar\"}})" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "id": "ae3a4fb9-2ce7-46b2-b654-35dff0ae7197", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "Looking at the LangSmith trace for this run, we can see that \"traced_key\" was recorded (as part of Metadata) while our secret int was not: https://smith.langchain.com/public/aa7e3289-49ca-422d-a408-f6b927210170/r" |
| 54 | + ] |
| 55 | + } |
| 56 | + ], |
| 57 | + "metadata": { |
| 58 | + "kernelspec": { |
| 59 | + "display_name": "poetry-venv-311", |
| 60 | + "language": "python", |
| 61 | + "name": "poetry-venv-311" |
| 62 | + }, |
| 63 | + "language_info": { |
| 64 | + "codemirror_mode": { |
| 65 | + "name": "ipython", |
| 66 | + "version": 3 |
| 67 | + }, |
| 68 | + "file_extension": ".py", |
| 69 | + "mimetype": "text/x-python", |
| 70 | + "name": "python", |
| 71 | + "nbconvert_exporter": "python", |
| 72 | + "pygments_lexer": "ipython3", |
| 73 | + "version": "3.11.9" |
| 74 | + } |
| 75 | + }, |
| 76 | + "nbformat": 4, |
| 77 | + "nbformat_minor": 5 |
| 78 | +} |
0 commit comments