Skip to content

Commit

Permalink
fix: Create new multipart encoder for each retry attempt (#1114)
Browse files Browse the repository at this point in the history
- requests doesn't seek the encoder object to the start when starting a
new retry, so we need to create a new encoder instead
  • Loading branch information
nfcampos authored Oct 23, 2024
2 parents 94a88ae + 4d3ed2f commit 6b63eca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
62 changes: 38 additions & 24 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1700,32 +1700,46 @@ def multipart_ingest_runs(
# send the request
self._send_multipart_req(acc_parts, _context="; ".join(acc_context))

def _send_multipart_req(self, parts: MultipartParts, *, _context: str):
def _send_multipart_req(
self, parts: MultipartParts, *, _context: str, attempts: int = 3
):
for api_url, api_key in self._write_api_urls.items():
try:
encoder = MultipartEncoder(parts, boundary=BOUNDARY)
self.request_with_retries(
"POST",
f"{api_url}/runs/multipart",
request_kwargs={
"data": encoder,
"headers": {
**self._headers,
X_API_KEY: api_key,
"Content-Type": encoder.content_type,
},
},
to_ignore=(ls_utils.LangSmithConflictError,),
stop_after_attempt=3,
_context=_context,
)
except Exception as e:
for idx in range(1, attempts + 1):
try:
exc_desc_lines = traceback.format_exception_only(type(e), e)
exc_desc = "".join(exc_desc_lines).rstrip()
logger.warning(f"Failed to multipart ingest runs: {exc_desc}")
except Exception:
logger.warning(f"Failed to multipart ingest runs: {repr(e)}")
encoder = MultipartEncoder(parts, boundary=BOUNDARY)
self.request_with_retries(
"POST",
f"{api_url}/runs/multipart",
request_kwargs={
"data": encoder,
"headers": {
**self._headers,
X_API_KEY: api_key,
"Content-Type": encoder.content_type,
},
},
stop_after_attempt=1,
_context=_context,
)
break
except ls_utils.LangSmithConflictError:
break
except (
ls_utils.LangSmithConnectionError,
ls_utils.LangSmithRequestTimeout,
ls_utils.LangSmithAPIError,
) as exc:
if idx == attempts:
logger.warning(f"Failed to multipart ingest runs: {exc}")
else:
continue
except Exception as e:
try:
exc_desc_lines = traceback.format_exception_only(type(e), e)
exc_desc = "".join(exc_desc_lines).rstrip()
logger.warning(f"Failed to multipart ingest runs: {exc_desc}")
except Exception:
logger.warning(f"Failed to multipart ingest runs: {repr(e)}")

def update_run(
self,
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langsmith"
version = "0.1.136"
version = "0.1.137"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 6b63eca

Please sign in to comment.