langchain-ai / langsmith-sdk

LangSmith Client SDK Implementations
https://docs.smith.langchain.com/
MIT License
422 stars 80 forks source link

support setting attachments through the RunTree object #1206

Open oegeshipster opened 1 week ago

oegeshipster commented 1 week ago

Feature request

so what i would like to do is the following inside a function that has a traceable decorator:

rt = get_current_run_tree()
pdf_attachment = Attachment(mime_type="application/pdf", data=pdf_data)
rt.attachments["pdf"] = pdf_attachment

but that currently does not work and the attachments do not show up in the dashboard.

So this this current example from the docs does work:

from langsmith import traceable, get_current_run_tree
from langsmith.schemas import Attachment

@traceable
def trace_with_attachments(
    val: int,
    text: str,
    pdf: Attachment,
):
    return f"Processed: {val}, {text},{len(pdf.data)}"

# Helper function to load files as bytes
def load_file(file_path: str) -> bytes:
    with open(file_path, "rb") as f:
        return f.read()

pdf_data = load_file(
    pdf_path
)
pdf_attachment = Attachment(mime_type="application/pdf", data=pdf_data)
# Define other parameters
val = 42
text = "Hello, world!"
# Call the function with traced attachments
result = trace_with_attachments(
    val=val,
    text=text,
    pdf=pdf_attachment,
)

but this does not:

@traceable
def trace_with_attachments(
    val: int,
    text: str,
    pdf_path: str,
):
    pdf_data = load_file(pdf_path)
    rt = get_current_run_tree()
    pdf_attachment = Attachment(mime_type="application/pdf", data=pdf_data)
    rt.attachments["pdf"] = pdf_attachment
    return f"Processed: {val}, {text},{len(pdf_data)}"

val = 42
text = "Hello, world!"
result = trace_with_attachments(
    val=val,
    text=text,
    pdf_path=pdf_path,
)

Motivation

currently I basically need to define a seperate wrapper function with Attachment parameters just to store the attachments. This adds wrapper functions and complication to my codebase, and makes it hard to use the traceable decorator directly on a fastapi route for example.