Skip to content

Commit

Permalink
Add langsmith client option to use the PyO3 bindings.
Browse files Browse the repository at this point in the history
I had to reduce the Python lower bound of the PyO3 bindings to 3.8 since Poetry refused to create a lockfile otherwise -- the langsmith project's own lower bound is 3.8.
  • Loading branch information
obi1kenobi committed Nov 26, 2024
1 parent 377aae8 commit a2df3b0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
51 changes: 50 additions & 1 deletion python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class Client:
"_write_api_urls",
"_settings",
"_manual_cleanup",
"_pyo3_client",
]

def __init__(
Expand Down Expand Up @@ -516,6 +517,51 @@ def __init__(
else ls_utils.get_env_var("HIDE_OUTPUTS") == "true"
)

# To set this up on the Rust side:
# - Navigate to the rust/crates/langsmith-pyo3 directory.
# - Run:
# maturin build --release
# - Then, back inside python/langsmith, run:
# poetry install --with pyo3
#
# To trigger this code, set the `LANGSMITH_USE_PYO3_CLIENT` env var to any value.
#
# Currently this requires Python 3.13 since the Poetry setup references the 3.13 wheel.
# Once the PyO3 package is uploaded to PyPI, we won't have to specify the *wheel* itself
# and can instead reference the *PyPI package name*. That will allow Poetry to choose
# a wheel that matches the locally-installed version of Python.
self._pyo3_client = None
if ls_utils.get_env_var("USE_PYO3_CLIENT") is not None:
langsmith_pyo3 = None
try:
import langsmith_pyo3
except ImportError as e:
logger.warning(
f"Failed to import `langsmith_pyo3` when PyO3 client was requested, "
f"falling back to Python impl: {repr(e)}",
)

if langsmith_pyo3:
# TODO: tweak these constants as needed
queue_capacity = 1_000_000
batch_size = 100
batch_timeout_millis = 1000
worker_threads = 1

try:
self._pyo3_client = langsmith_pyo3.BlockingTracingClient(
self.api_url,
queue_capacity,
batch_size,
batch_timeout_millis,
worker_threads,
)
except Exception as e:
logger.warning(
f"Failed to instantiate `langsmith_pyo3.BlockingTracingClient` "
f"when PyO3 client was requested, falling back to Python impl: {repr(e)}",
)

self._settings: Union[ls_schemas.LangSmithSettings, None] = None

self._manual_cleanup = False
Expand Down Expand Up @@ -1226,7 +1272,10 @@ def create_run(
copy=False,
)
self._insert_runtime_env([run_create])
if (

if self._pyo3_client is not None:

This comment has been minimized.

Copy link
@agola11

agola11 Nov 26, 2024

Contributor

should check if trace_id and dotted_order is present in this check as well, otherwise the downstream multipart endpoint we use in the pyo3 client will fail.

This comment has been minimized.

Copy link
@obi1kenobi

obi1kenobi Nov 26, 2024

Author Collaborator

Ah got it, I'll nest this check one step deeper then.

This comment has been minimized.

Copy link
@obi1kenobi

obi1kenobi Nov 26, 2024

Author Collaborator

Fixed in fbc4736

self._pyo3_client.create_run(run_create)
elif (
self.tracing_queue is not None
# batch ingest requires trace_id and dotted_order to be set
and run_create.get("trace_id") is not None
Expand Down
18 changes: 16 additions & 2 deletions python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ openai = "^1.10"
[tool.poetry.group.test.dependencies]
pytest-socket = "^0.7.0"

[tool.poetry.group.pyo3.dependencies]
langsmith-pyo3 = {path = "../rust/target/wheels/langsmith_pyo3-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl"}

[tool.poetry.extras]
vcr = ["vcrpy"]

Expand Down
2 changes: 1 addition & 1 deletion rust/crates/langsmith-pyo3/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "langsmith-pyo3"
requires-python = ">=3.9"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
Expand Down

0 comments on commit a2df3b0

Please sign in to comment.