Skip to content

Commit 5fbb445

Browse files
Napuhix-56h
andauthored
fix: handle network errors gracefully in token count estimation (#437)
Co-authored-by: ix-56h <n.guintini@protonmail.com> Co-authored-by: Zarial <39010759+ix-56h@users.noreply.github.com>
1 parent efe5a26 commit 5fbb445

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ repos:
2626

2727
- id: trailing-whitespace
2828
description: 'Trim trailing whitespace.'
29+
exclude: CHANGELOG.md
2930

3031
- id: check-docstring-first
3132
description: 'Check a common error of defining a docstring after code.'

src/gitingest/output_formatter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
from __future__ import annotations
44

5+
import ssl
6+
import warnings
57
from typing import TYPE_CHECKING
68

9+
import requests.exceptions
710
import tiktoken
811

912
from gitingest.schemas import FileSystemNode, FileSystemNodeType
@@ -190,7 +193,11 @@ def _format_token_count(text: str) -> str | None:
190193
encoding = tiktoken.get_encoding("o200k_base") # gpt-4o, gpt-4o-mini
191194
total_tokens = len(encoding.encode(text, disallowed_special=()))
192195
except (ValueError, UnicodeEncodeError) as exc:
193-
print(exc)
196+
warnings.warn(f"Failed to estimate token size: {exc}", RuntimeWarning, stacklevel=3)
197+
return None
198+
except (requests.exceptions.RequestException, ssl.SSLError) as exc:
199+
# If network errors, skip token count estimation instead of erroring out
200+
warnings.warn(f"Failed to download tiktoken model: {exc}", RuntimeWarning, stacklevel=3)
194201
return None
195202

196203
for threshold, suffix in _TOKEN_THRESHOLDS:

0 commit comments

Comments
 (0)