Skip to content
This repository was archived by the owner on Nov 11, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 95 additions & 20 deletions python/langsmith/run_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ def _format_error_with_exceptions_to_handle(
if exceptions_to_handle and isinstance(error, exceptions_to_handle):
return None

stacktrace = utils._format_exc()
stacktrace = utils._format_exc(error)
return f"{repr(error)}\n\n{stacktrace}"


Expand Down Expand Up @@ -2283,10 +2283,11 @@ def __init__(
stream: Union[Iterator[T], AsyncIterator[T]],
trace_container: _TraceableContainer,
reduce_fn: Optional[Callable] = None,
):
) -> None:
self.__ls_stream__ = stream
self.__ls_trace_container__ = trace_container
self.__ls_completed__ = False
self.__ls_context_depth__ = 0
self.__ls_reduce_fn__ = reduce_fn
self.__ls_accumulated_output__: list[T] = []
self.__is_llm_run__ = (
Expand All @@ -2295,8 +2296,55 @@ def __init__(
else False
)

def __getattr__(self, name: str):
return getattr(self.__ls_stream__, name)
def __getattr__(self, name: str) -> Any:
attribute: Any = getattr(self.__ls_stream__, name)
if name not in ("close", "aclose") or not callable(attribute):
return attribute

close: Callable[..., Any] = functools.partial(self._close_stream, attribute)
if inspect.iscoroutinefunction(attribute):

@functools.wraps(attribute)
async def async_close(*args: Any, **kwargs: Any) -> Any:
return await close(*args, **kwargs)

return async_close
return functools.wraps(attribute)(close)

def _close_stream(
self, close: Callable[..., Any], /, *args: Any, **kwargs: Any
) -> Any:
try:
result: Any = close(*args, **kwargs)
except BaseException as error:
_cleanup_traceback(error)
if self.__ls_context_depth__ == 0:
self._end_trace(error=error)
raise
if inspect.isawaitable(result):
return self._await_close_result(result)
if self.__ls_context_depth__ == 0:
self._end_trace()
return result

async def _await_close_result(self, awaitable: Awaitable[Any]) -> Any:
try:
result: Any = await awaitable
except BaseException as error:
_cleanup_traceback(error)
if self.__ls_context_depth__ == 0:
await self._aend_trace(error=error)
raise
if self.__ls_context_depth__ == 0:
await self._aend_trace()
return result

async def _aend_trace(self, error: Optional[BaseException] = None) -> None:
ctx: contextvars.Context = copy_context()
await asyncio.shield(
aitertools.aio_to_thread(ctx, self._end_trace, error=error)
)
_set_tracing_context(get_tracing_context(ctx))

def __dir__(self):
return list(set(dir(self.__class__) + dir(self.__ls_stream__)))
Expand Down Expand Up @@ -2382,9 +2430,14 @@ def __enter__(self):

def __exit__(self, exc_type, exc_val, exc_tb):
try:
return self.__ls_stream__.__exit__(exc_type, exc_val, exc_tb)
finally:
self._end_trace(error=exc_val if exc_type else None)
result = self.__ls_stream__.__exit__(exc_type, exc_val, exc_tb)
except BaseException as e:
_cleanup_traceback(e)
self._end_trace(error=e)
raise
else:
self._end_trace(error=None if result else exc_val if exc_type else None)
return result


class _TracedAsyncStream(_TracedStreamBase, Generic[T]):
Expand Down Expand Up @@ -2412,37 +2465,59 @@ def __init__(
process_chunk=process_chunk,
)

async def _aend_trace(self, error: Optional[BaseException] = None):
ctx = copy_context()
await asyncio.shield(aitertools.aio_to_thread(ctx, self._end_trace, error))
_set_tracing_context(get_tracing_context(ctx))

async def __anext__(self) -> T:
try:
return cast(T, await aitertools.py_anext(self.__ls_gen))
except StopAsyncIteration:
await self._aend_trace()
if self.__ls_context_depth__ == 0:
await self._aend_trace()
raise
except BaseException as e:
_cleanup_traceback(e)
if self.__ls_context_depth__ == 0:
await self._aend_trace(error=e)
raise

async def __aiter__(self) -> AsyncIterator[T]:
try:
async for item in self.__ls_gen:
yield item
except BaseException:
await self._aend_trace()
except BaseException as e:
_cleanup_traceback(e)
if self.__ls_context_depth__ == 0:
await self._aend_trace(error=e)
raise
else:
await self._aend_trace()
if self.__ls_context_depth__ == 0:
await self._aend_trace()

async def __aenter__(self):
await self.__ls_stream__.__aenter__()
try:
await self.__ls_stream__.__aenter__()
except BaseException as e:
_cleanup_traceback(e)
if self.__ls_context_depth__ == 0:
await self._aend_trace(error=e)
raise
self.__ls_context_depth__ += 1
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
try:
return await self.__ls_stream__.__aexit__(exc_type, exc_val, exc_tb)
finally:
await self._aend_trace()
result = await self.__ls_stream__.__aexit__(exc_type, exc_val, exc_tb)
except BaseException as e:
_cleanup_traceback(e)
self.__ls_context_depth__ -= 1
if self.__ls_context_depth__ == 0:
await self._aend_trace(error=e)
raise
else:
self.__ls_context_depth__ -= 1
if self.__ls_context_depth__ == 0:
await self._aend_trace(
error=None if result else exc_val if exc_type else None
)
return result


def _get_function_result(results: list, reduce_fn: Callable) -> Any:
Expand Down
8 changes: 6 additions & 2 deletions python/langsmith/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,13 @@ def with_optional_cache(
yield


def _format_exc() -> str:
def _format_exc(error: BaseException | None = None) -> str:
# Used internally to format exceptions without cluttering the traceback
tb_lines = traceback.format_exception(*sys.exc_info())
tb_lines = (
traceback.format_exception(type(error), error, error.__traceback__)
if error is not None
else traceback.format_exception(*sys.exc_info())
)
filtered_lines = [line for line in tb_lines if "langsmith/" not in line]
return "".join(filtered_lines)

Expand Down
77 changes: 77 additions & 0 deletions python/tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,85 @@

import json
import re
import threading
from collections.abc import Iterator
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Any, Dict, List

import pytest


class HttpEndpoint:
def __init__(self) -> None:
self.count = 0
self.status: int = HTTPStatus.OK
self.status_by_path: dict[str, int] = {}
self.retry_after: int | None = None
self.url = ""
self.responses: dict[str, tuple[str, bytes]] = {}
self.requests: list[tuple[str, str, bytes]] = []

@property
def paths(self) -> list[str]:
return [path for _, path, _ in self.requests]


@pytest.fixture
def endpoint() -> Iterator[HttpEndpoint]:
ep = HttpEndpoint()

class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"

def do_POST(self) -> None: # noqa: N802
# Drain the body first, otherwise the client sees a reset rather
# than the status code we are trying to exercise.
request_body = b""
length = int(self.headers.get("Content-Length") or 0)
if length:
request_body = self.rfile.read(length)
elif self.headers.get("Transfer-Encoding") == "chunked":
while True:
size = int(self.rfile.readline().strip() or b"0", 16)
request_body += self.rfile.read(size)
self.rfile.read(2)
if size == 0:
break
ep.count += 1
ep.requests.append((self.command, self.path, request_body))
content_type, body = ep.responses.get(
self.path, ("application/json", b"{}")
)
self.send_response(ep.status_by_path.get(self.path, ep.status))
if ep.retry_after is not None:
self.send_header("Retry-After", str(ep.retry_after))
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)

do_PATCH = do_POST

def log_message(self, format: str, *args: object) -> None:
pass

server = ThreadingHTTPServer(
server_address=("127.0.0.1", 0), RequestHandlerClass=Handler
)
# The default 0.5s polling interval would dominate fixture teardown.
thread = threading.Thread(
target=server.serve_forever, kwargs={"poll_interval": 0.001}, daemon=True
)
thread.start()
ep.url = f"http://127.0.0.1:{server.server_port}"
try:
yield ep
finally:
server.shutdown()
server.server_close()
thread.join()


def parse_multipart_data(data: bytes) -> Dict[str, Any]:
"""Parse multipart form data into a batch-like dict structure.
Expand Down
59 changes: 1 addition & 58 deletions python/tests/unit_tests/test_multipart_retry_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import json
import logging
import re
import threading
import uuid
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from unittest import mock

import pytest
Expand All @@ -36,62 +34,7 @@
)
from langsmith.client import Client, _close_files


class _Endpoint:
"""A stand-in /runs/multipart that returns a canned status and counts hits."""

def __init__(self):
self.count = 0
self.status = 200
self.status_by_path: dict[str, int] = {}
self.retry_after = None
self.url = ""
self.paths: list[str] = []


@pytest.fixture
def endpoint(socket_enabled):
ep = _Endpoint()

class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"

def do_POST(self): # noqa: N802
# Drain the body first, otherwise the client sees a reset rather
# than the status code we are trying to exercise.
length = int(self.headers.get("Content-Length") or 0)
if length:
self.rfile.read(length)
elif self.headers.get("Transfer-Encoding") == "chunked":
while True:
size = int(self.rfile.readline().strip() or b"0", 16)
self.rfile.read(size + 2)
if size == 0:
break
ep.count += 1
ep.paths.append(self.path)
body = b"{}"
self.send_response(ep.status_by_path.get(self.path, ep.status))
if ep.retry_after is not None:
self.send_header("Retry-After", str(ep.retry_after))
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)

def log_message(self, *args):
pass

server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
# poll_interval bounds how long shutdown() blocks. The 0.5s default would
# dominate this file's runtime; 0 would work too but busy-spins.
threading.Thread(
target=server.serve_forever, kwargs={"poll_interval": 0.001}, daemon=True
).start()
ep.url = f"http://127.0.0.1:{server.server_address[1]}"
yield ep
server.shutdown()
server.server_close()
pytestmark = pytest.mark.allow_hosts(["127.0.0.1"])


@pytest.fixture
Expand Down
Loading