Skip to content

Commit 1d5091e

Browse files
authored
Cache retry config (#1622)
Since ``` urllib3_version = importlib.metadata.version("urllib3") ``` Calls os.stat under the hood --------- Signed-off-by: William Fu-Hinthorn <[email protected]>
1 parent 09e696b commit 1d5091e

File tree

8 files changed

+203
-30
lines changed

8 files changed

+203
-30
lines changed

python/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ profile-background-thread:
2222
view-profile:
2323
poetry run snakeviz profiles/${PROFILE_NAME}.prof
2424

25+
TEST ?= "tests/unit_tests"
2526
tests:
2627
env \
2728
-u LANGCHAIN_PROJECT \
@@ -30,16 +31,17 @@ tests:
3031
-u LANGSMITH_TRACING \
3132
PYTHONDEVMODE=1 \
3233
PYTHONASYNCIODEBUG=1 \
33-
poetry run python -m pytest --disable-socket --allow-unix-socket -n auto --durations=10 tests/unit_tests
34+
poetry run python -m pytest --disable-socket --allow-unix-socket -n auto --durations=10 ${TEST}
3435

3536
tests_watch:
3637
poetry run ptw --now . -- -vv -x tests/unit_tests
3738

39+
INT_TEST ?= "tests/integration_tests"
3840
integration_tests:
39-
poetry run python -m pytest -x -v --durations=10 --cov=langsmith --cov-report=term-missing --cov-report=html --cov-config=.coveragerc tests/integration_tests
41+
poetry run python -m pytest -x -v --durations=10 --cov=langsmith --cov-report=term-missing --cov-report=html --cov-config=.coveragerc ${INT_TEST}
4042

4143
integration_tests_fast:
42-
poetry run python -m pytest -x -n auto --durations=10 -v --cov=langsmith --cov-report=term-missing --cov-report=html --cov-config=.coveragerc tests/integration_tests
44+
poetry run python -m pytest -x -n auto --durations=10 -v --cov=langsmith --cov-report=term-missing --cov-report=html --cov-config=.coveragerc ${INT_TEST}
4345

4446
doctest:
4547
poetry run python -m pytest -n auto -x --durations=10 --doctest-modules langsmith

python/langsmith/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def _is_langchain_hosted(url: str) -> bool:
217217
]
218218

219219

220+
@functools.lru_cache(maxsize=1)
220221
def _default_retry_config() -> Retry:
221222
"""Get the default retry configuration.
222223
@@ -7672,6 +7673,8 @@ def _convert_stored_attachments_to_attachments_dict(
76727673
attachments_dict = {}
76737674
if attachments_key in data and data[attachments_key]:
76747675
for key, value in data[attachments_key].items():
7676+
if not key.startswith("attachment."):
7677+
continue
76757678
response = requests.get(value["presigned_url"], stream=True)
76767679
response.raise_for_status()
76777680
reader = io.BytesIO(response.content)

python/langsmith/run_helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,8 +1583,12 @@ def _get_inputs_and_attachments_safe(
15831583
return {"args": args, "kwargs": kwargs}, {}
15841584

15851585

1586-
def _set_tracing_context(context: Dict[str, Any]):
1586+
def _set_tracing_context(context: Optional[Dict[str, Any]] = None):
15871587
"""Set the tracing context."""
1588+
if context is None:
1589+
for k, v in _CONTEXT_KEYS.items():
1590+
v.set(None)
1591+
return
15881592
for k, v in context.items():
15891593
var = _CONTEXT_KEYS[k]
15901594
var.set(v)

python/poetry.lock

Lines changed: 175 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "langsmith"
3-
version = "0.3.20"
3+
version = "0.3.21"
44
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
55
authors = ["LangChain <[email protected]>"]
66
license = "MIT"
@@ -76,6 +76,7 @@ multipart = "^1.0.0"
7676
# TODO: remove
7777
httpx = ">=0.23.0,<0.28.0"
7878
rich = "^13.9.4"
79+
pytest-retry = "^1.7.0"
7980

8081
[tool.poetry.group.lint.dependencies]
8182
openai = "^1.55"

python/tests/integration_tests/test_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ def test_multipart_ingest_create_with_attachments_error(
925925
langchain_client.multipart_ingest(create=runs_to_create, update=[])
926926

927927

928+
@pytest.mark.flaky(retries=3)
928929
def test_multipart_ingest_create_with_attachments(
929930
langchain_client: Client, caplog: pytest.LogCaptureFixture
930931
) -> None:
@@ -958,10 +959,13 @@ def test_multipart_ingest_create_with_attachments(
958959
langchain_client.multipart_ingest(
959960
create=runs_to_create, update=[], dangerously_allow_filesystem=True
960961
)
962+
langchain_client.flush()
961963
assert not caplog.records
962964
wait_for(lambda: _get_run(str(trace_a_id), langchain_client))
963965
created_run = langchain_client.read_run(run_id=str(trace_a_id))
964-
assert sorted(created_run.attachments.keys()) == sorted(["foo", "bar"])
966+
assert sorted(created_run.attachments.keys()) == sorted(
967+
["foo", "bar"]
968+
), f"See failed run at {created_run.url}"
965969
assert created_run.attachments["foo"]["reader"].read() == b"bar"
966970
assert (
967971
created_run.attachments["bar"]["reader"].read()

python/tests/unit_tests/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ def filter_outputs(outputs: dict):
679679
assert all([exp in all_posted for exp in expected])
680680

681681

682+
@pytest.mark.flaky(retries=3)
682683
def test_client_gc_after_autoscale() -> None:
683684
session = mock.MagicMock(spec=requests.Session)
684685
client = Client(

python/tests/unit_tests/test_run_helpers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,7 @@ def consume(gen):
17311731
assert list(consume(wrapped)) == list(range(5))
17321732

17331733

1734+
@pytest.mark.flaky(retries=3)
17341735
def test_traceable_input_attachments():
17351736
with patch.object(ls_client.ls_env, "get_runtime_environment") as mock_get_env:
17361737
mock_get_env.return_value = {
@@ -1751,14 +1752,17 @@ def my_func(
17511752

17521753
mock_client = _get_mock_client(
17531754
info=ls_schemas.LangSmithInfo(
1755+
instance_flags={
1756+
"zstd_compression_enabled": False,
1757+
},
17541758
batch_ingest_config=ls_schemas.BatchIngestConfig(
17551759
use_multipart_endpoint=True,
17561760
size_limit_bytes=None, # Note this field is not used here
17571761
size_limit=100,
17581762
scale_up_nthreads_limit=16,
17591763
scale_up_qsize_trigger=1000,
17601764
scale_down_nempty_trigger=4,
1761-
)
1765+
),
17621766
),
17631767
)
17641768
long_content = b"c" * 20_000_000
@@ -1771,6 +1775,8 @@ def my_func(
17711775
)
17721776
assert result == "foo"
17731777

1778+
mock_client.flush()
1779+
17741780
for _ in range(20):
17751781
calls = _get_calls(mock_client)
17761782
datas = _get_multipart_data(calls)

0 commit comments

Comments
 (0)