wrap_anthropic#

langsmith.wrappers._anthropic.wrap_anthropic(client: C, *, tracing_extra: TracingExtra | None = None) C[source]#

Patch the Anthropic client to make it traceable.

Parameters:
  • client (Union[Anthropic, AsyncAnthropic]) – The client to patch.

  • tracing_extra (Optional[TracingExtra], optional) – Extra tracing information. Defaults to None.

Returns:

The patched client.

Return type:

Union[Anthropic, AsyncAnthropic]

Example

import anthropic
from langsmith import wrappers

client = wrappers.wrap_anthropic(anthropic.Anthropic())

# Use Anthropic client same as you normally would:
system = "You are a helpful assistant."
messages = [
    {
        "role": "user",
        "content": "What physics breakthroughs do you predict will happen by 2300?",
    }
]
completion = client.messages.create(
    model="claude-3-5-sonnet-latest",
    messages=messages,
    max_tokens=1000,
    system=system,
)
print(completion.content)

# You can also use the streaming context manager:
with client.messages.stream(
    model="claude-3-5-sonnet-latest",
    messages=messages,
    max_tokens=1000,
    system=system,
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
    message = stream.get_final_message()