There are three ways to add metadata to logs and traces.

  1. The first is to add metadata directly to Parea’s completion method.
  2. The second is to add metadata to the trace decorator.
  3. Finally, for the Python SDK, if your trace has already been created, and you want to add dynamic metadata, you can use the trace_insert helper function.

Completion method

The completion method supports the following metadata fields:

  • trace_name - default is ‘LLM’
  • end_user_identifier - This is a string representing any identifier for your end users such as an email address or user id.
  • metadata - This is a serializable dictionary of key value pairs that you want added to the trace. ex: {'git-hash': '1234abcd'}
  • tags - This is a list of strings that you want added to the trace. ex: ['tag1', 'tag2']
  • target - A string representing the ground truth answer you expect from the LLM
completion_with_metadata = Completion(
    trace_name="custom_trace_name",
    end_user_identifier="user_id_123",
    metadata={"git-hash": "1234awbcd"},
    tags=["tag1", "tag2"],
    target="ground_truth_answer",
)

Trace decorator

The trace decorator supports the same metadata fields as the completion method.

  • name - default is the function’s name
  • end_user_identifier
  • metadata
  • tags
  • target
  • deployment_id
  • session_id - A string representing a session id for the completion.
@trace(
    name="custom_trace_name",
    end_user_identifier="user_id_123",
    metadata={"git-hash": "1234abcd"},
    tags=["tag1", "tag2"],
    target="ground_truth_answer",
    deployment_id="deployment_id"
    session_id="session_id"
)
def my_function():
    ...

Trace insert

Sometimes the metadata you want to add to a trace is based on something that happens within the function. In this case you could use the trace_insert() helper function to add metadata to the trace.

trace_insert takes a dictionary of metadata field name to value. The fields supported by trace_insert are:

  • trace_name
  • end_user_identifier
  • metadata
  • tags
  • deployment_id
  • session_id - A string representing a session id for the completion.

The new data will be added to the most recent trace.

from parea import trace, trace_insert

@trace
def get_sections(chapter: Dict[str, str], model: str):
    # I want the trace name to be dynamic and based on the chapter dictionary passed in,
    # so I can use trace_insert helper method to add dynamic data
    trace_insert({"trace_name": f"Get {chapter['name']} Sections"})

    return client.chat.completions.create(
        model=model,
        temperature=0.7,
        messages=[
            {
                "role": "user",
                "content": f"Generate a section outline on {chapter['name']}, with description {chapter['description']}",
            }
        ],
    )

What’s Next

Now that we have added metadata to our traces, we add user feedback scores to our traces.