get_conversation_metadata
Read a value stored on the current conversation, such as a user ID passed in from your website.
get_conversation_metadata(context: dict, key: str) -> str | NoneReads a metadata value stored on the conversation, or None if the key doesn't exist. Metadata is how your flows remember facts across turns. For example, an action can read the logged-in user's ID that your website passed in through chat custom data.
Parameters
| Name | Type | Description |
|---|---|---|
context | dict | The context object passed to your action |
key | str | The metadata key to retrieve |
Returns
str | None: the stored value, or None if the key is not found.
Behavior
- Returns
Nonein test/mock environments (whencontext["conversation"]is absent). - Metadata keys are set via
set_conversation_metadataor through Chat custom data (keys prefixed withchat-widget:).
Examples
Read a user ID from chat custom data
def execute_action(context):
user_id = get_conversation_metadata(context, "chat-widget:user_id")
if not user_id:
return {"error": "No user ID found. The customer may not be logged in."}
response = requests.get(
"https://api.example.com/users",
params={"id": user_id},
headers={"Authorization": "Bearer API_KEY"},
timeout=30,
)
response.raise_for_status()
return response.json()Check if a previous action already ran
def execute_action(context):
already_refunded = get_conversation_metadata(context, "refund_processed")
if already_refunded:
return {"error": "A refund has already been processed in this conversation."}
# ... process refund ...
set_conversation_metadata(context, "refund_processed", "true")
return {"success": True}