Skip to content

Commit 08e9a4f

Browse files
committed
docs: expand normalize_path docstring with usage and examples
- Add detailed explanation of function's purpose and usage contexts\n- Document rationale for stripping leading/trailing slashes\n- Add comprehensive examples for different path formats\n- Reference SyncClient.pull usage for context
1 parent a9de255 commit 08e9a4f

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/humanloop/path_utils.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,45 @@
22

33

44
def normalize_path(path: str, strip_extension: bool = False) -> str:
5-
"""Normalize a path to the standard API format: path/to/resource, where resource can be a file or a directory."""
5+
"""Normalize a path to the standard Humanloop API format.
6+
7+
This function is primarily used when interacting with the Humanloop API to ensure paths
8+
follow the standard format: 'path/to/resource' without leading/trailing slashes.
9+
It's used in several contexts:
10+
1. When pulling files from Humanloop to local filesystem (see SyncClient.pull)
11+
2. When making API calls that reference files by path
12+
3. When handling local file operations that need to match API path conventions
13+
14+
The function:
15+
- Converts Windows backslashes to forward slashes
16+
- Normalizes consecutive slashes
17+
- Optionally strips file extensions (e.g. .prompt, .agent)
18+
- Removes leading/trailing slashes to match API conventions
19+
20+
Leading/trailing slashes are stripped because the Humanloop API expects paths in the
21+
format 'path/to/resource' without them. This is consistent with how the API stores
22+
and references files, and ensures paths work correctly in both API calls and local
23+
filesystem operations.
24+
25+
Args:
26+
path: The path to normalize. Can be a Windows or Unix-style path.
27+
strip_extension: If True, removes the file extension (e.g. .prompt, .agent)
28+
29+
Returns:
30+
Normalized path string in the format 'path/to/resource'
31+
32+
Examples:
33+
>>> normalize_path("path/to/file.prompt")
34+
'path/to/file.prompt'
35+
>>> normalize_path("path/to/file.prompt", strip_extension=True)
36+
'path/to/file'
37+
>>> normalize_path("\\windows\\style\\path.prompt")
38+
'windows/style/path.prompt'
39+
>>> normalize_path("/leading/slash/path/")
40+
'leading/slash/path'
41+
>>> normalize_path("multiple//slashes//path")
42+
'multiple/slashes/path'
43+
"""
644
# Handle backslashes for Windows paths before passing to PurePosixPath
745
# This is needed because some backslash sequences are treated as escape chars
846
path = path.replace("\\", "/")

0 commit comments

Comments
 (0)