Skip to content

Commit

Permalink
Address comments + mask api key
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Huang authored and Nick Huang committed Oct 9, 2024
1 parent e5d1415 commit d946180
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import collections
import concurrent.futures as cf
import contextlib
import copy
import datetime
import decimal
import functools
import gzip
import importlib
import importlib.metadata
import io
Expand Down Expand Up @@ -731,11 +733,7 @@ def _get_settings(self) -> ls_schemas.LangSmithSettings:

return self._settings

def _content_above_size(
self,
content_length: Optional[int],
request: Optional[requests.Request | requests.PreparedRequest],
) -> Optional[str]:
def _content_above_size(self, content_length: Optional[int]) -> Optional[str]:
if content_length is None or self._info is None:
return None
info = cast(ls_schemas.LangSmithInfo, self._info)
Expand All @@ -746,14 +744,6 @@ def _content_above_size(
if size_limit is None:
return None
if content_length > size_limit:
should_debug_crash_dump = os.getenv("LANGSMITH_DEBUG_CRASH_DUMP") in [
"1",
"true",
]
if should_debug_crash_dump and request is not None:
with open("content_size_limit_crash_dump.jsonl", "a") as f:
json.dump(request, f)
f.write("\n")
return (
f"The content length of {content_length} bytes exceeds the "
f"maximum size limit of {size_limit} bytes."
Expand Down Expand Up @@ -930,7 +920,7 @@ def request_with_retries(
if e.request
else ""
)
size_rec = self._content_above_size(content_length, e.request)
size_rec = self._content_above_size(content_length)
if size_rec:
recommendation = size_rec
except ValueError:
Expand All @@ -943,6 +933,17 @@ def request_with_retries(
filler = "*" * (max(0, len(api_key) - 7))
masked_api_key = f"{prefix}{filler}{suffix}"

debug_crash_dump_file = ls_utils.get_env_var(
"LANGSMITH_DEBUG_CRASH_DUMP"
)
if debug_crash_dump_file is not None:
request_data = copy.deepcopy(e.request) if e.request else {}
if "x-api-key" in request_data.get("headers", {}):
request_data["headers"]["x-api-key"] = masked_api_key
with gzip.open(debug_crash_dump_file, "ab") as f:
json_data = json.dumps(request_data).encode("utf-8")
f.write(json_data + b"\n")

raise ls_utils.LangSmithConnectionError(
f"Connection error caused failure to {method} {pathname}"
f" in LangSmith API. {recommendation}"
Expand Down

0 comments on commit d946180

Please sign in to comment.