From 63e55f76364eec62e155cf4a8b0e3c42b295185d Mon Sep 17 00:00:00 2001 From: Angus Jelinek Date: Thu, 19 Dec 2024 13:08:14 -0800 Subject: [PATCH] Use a single join() rather than copying the header strings --- python/langsmith/_internal/_operations.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/python/langsmith/_internal/_operations.py b/python/langsmith/_internal/_operations.py index fca108796..f75411084 100644 --- a/python/langsmith/_internal/_operations.py +++ b/python/langsmith/_internal/_operations.py @@ -274,26 +274,27 @@ def serialized_run_operation_to_multipart_parts_and_context( f"trace={op.trace_id},id={op.id}", ) - def compress_multipart_parts_and_context( parts_and_context: MultipartPartsAndContext, compressor_writer: zstd.ZstdCompressionWriter, boundary: str, ) -> None: for part_name, (filename, data, content_type, headers) in parts_and_context.parts: - part_header = f"--{boundary}\r\n" - part_header += f'Content-Disposition: form-data; name="{part_name}"' + header_parts = [ + f"--{boundary}\r\n", + f'Content-Disposition: form-data; name="{part_name}"' + ] if filename: - part_header += f'; filename="{filename}"' - - part_header += f"\r\nContent-Type: {content_type}\r\n" + header_parts.append(f'; filename="{filename}"') - for header_name, header_value in headers.items(): - part_header += f"{header_name}: {header_value}\r\n" + header_parts.extend([ + f"\r\nContent-Type: {content_type}\r\n", + *[f"{k}: {v}\r\n" for k, v in headers.items()], + "\r\n" + ]) - part_header += "\r\n" - compressor_writer.write(part_header.encode()) + compressor_writer.write("".join(header_parts).encode()) if isinstance(data, (bytes, bytearray)): compressor_writer.write(data)