|
| 1 | +--- |
| 2 | +hide: |
| 3 | + - toc |
| 4 | +--- |
| 5 | + |
| 6 | +# Append Messages API |
| 7 | + |
| 8 | +<div class='subtitle'>Append new messages to an existing trace</div> |
| 9 | + |
| 10 | +The Append Messages API allows you to append new messages to an existing trace in a programmatic way. Within a trace, all the messages are stored as a list of messages. |
| 11 | + |
| 12 | +The SDK includes a timestamp field by default (set to the current time) when calling the API. This timestamp field is used to perform a sorted insert of the new messages into the list of existing messages in the trace. |
| 13 | + |
| 14 | +It is possible some of the existing messages in the trace don't have any timestamp - in that case the trace creation timestamp is used for the comparison. |
| 15 | + |
| 16 | +This API works on traces within datasets and on standalone traces (snippets) without any datasets. |
| 17 | + |
| 18 | +## Data Types |
| 19 | + |
| 20 | +### `AppendMessagesRequest` |
| 21 | + |
| 22 | +The `AppendMessagesRequest` class holds the request data for a messages append request. |
| 23 | + |
| 24 | +##### `messages` <span class='type'>List[Dict]</span> <span class='required'/> |
| 25 | + |
| 26 | +This represents the new messages to append to the trace. Each `dict` is a single message within a trace - these can represet a user prompt, a tool call, a tool output, etc. |
| 27 | + |
| 28 | +Must be in the [required trace format](../trace-format.md). Must not be empty. |
| 29 | + |
| 30 | +##### `trace_id` <span class='type'>str</span> <span class='required'/> |
| 31 | + |
| 32 | +The id of the trace to which you want to append messages. This has to exist before the Append Messages API can be called and the caller must be the owner of the trace or the dataset containing the trace. |
| 33 | + |
| 34 | + |
| 35 | +## Pushing Traces |
| 36 | + |
| 37 | +There are two SDK methods to push traces: `append_messages` and `create_request_and_append_messages`. The former accepts the `AppendMessagesRequest` type as an argument and the latter accepts Python-native types as arguments. |
| 38 | + |
| 39 | +### `append_messages` |
| 40 | +The `append_messages` method is used to append messages to an existing trace using a pre-constructed request object. |
| 41 | + |
| 42 | +##### `request` <span class='type'>AppendMessagesRequest</span> <span class='required'/> |
| 43 | + |
| 44 | +The request object containing new messages and the trace id. |
| 45 | + |
| 46 | +##### `request_kwargs` <span class='type'>Optional[Dict[str, Any]]</span> <span class='optional'/> |
| 47 | + |
| 48 | +Additional keyword arguments to pass to the requests method. Default is `None`. |
| 49 | + |
| 50 | +##### Return Value |
| 51 | + |
| 52 | +##### <span class='type'>Dict</span> |
| 53 | + |
| 54 | +The response object from the Invariant API. |
| 55 | + |
| 56 | +> AsyncClient Example |
| 57 | + ```python |
| 58 | + from invariant_sdk.async_client import AsyncClient |
| 59 | + from invariant_sdk.types.append_messages import PushTracesRequest |
| 60 | + |
| 61 | + client = AsyncClient() |
| 62 | + |
| 63 | + request = PushTracesRequest( |
| 64 | + messages=[ |
| 65 | + {"role": "user", "content": "one"}, |
| 66 | + {"role": "assistant", "content": "two \n three"}, |
| 67 | + ], |
| 68 | + trace_id="some_trace_id" |
| 69 | + ) |
| 70 | + |
| 71 | + response = await client.append_messages(request) |
| 72 | + ``` |
| 73 | + |
| 74 | +### `create_request_and_append_messages` |
| 75 | + |
| 76 | +The `create_request_and_append_messages` method is used to append messages to an existing trace. It creates a request object from the provided messages and trace id pushes this data to the API. |
| 77 | + |
| 78 | +##### `messages` <span class='type'>List[Dict]</span> <span class='required'/> |
| 79 | + |
| 80 | +This represents the new messages to append to the trace. Each `dict` is a single message within a trace - these can represet a user prompt, a tool call, a tool output, etc. |
| 81 | + |
| 82 | +Must be in the [required trace format](../trace-format.md). Must not be empty. |
| 83 | + |
| 84 | +##### `trace_id` <span class='type'>str</span> <span class='required'/> |
| 85 | + |
| 86 | +The id of the trace to which you want to append messages. This has to exist before the Append Messages API can be called and the caller must be the owner of the trace or the dataset containing the trace. |
| 87 | + |
| 88 | +##### `request_kwargs` <span class='type'>Optional[Mapping]</span> <span class='optional'/> |
| 89 | + |
| 90 | +Additional keyword arguments to pass to the requests method. Default is `None`. |
| 91 | + |
| 92 | +##### Return Value |
| 93 | + |
| 94 | +##### <span class='type'>Dict</span> |
| 95 | + |
| 96 | +The response object from the Invariant API. |
| 97 | + |
| 98 | +> AsyncClient Example |
| 99 | + ```python |
| 100 | + from invariant_sdk.async_client import AsyncClient |
| 101 | + |
| 102 | + client = AsyncClient() |
| 103 | + |
| 104 | + messages = [ |
| 105 | + {"role": "user", "content": "one"}, |
| 106 | + {"role": "assistant", "content": "two \n three"}, |
| 107 | + ] |
| 108 | + |
| 109 | + response = await client.create_request_and_push_trace( |
| 110 | + messages=messages, |
| 111 | + trace_id="some_trace_id" |
| 112 | + ) |
| 113 | + ``` |
0 commit comments