fix(proxy): honor MAX_STRING_LENGTH_PROMPT_IN_DB from config env vars - #22106
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes issue #22088 where
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/proxy/spend_tracking/spend_tracking_utils.py | Adds _get_max_string_length_prompt_in_db() to resolve the truncation limit from os.environ at runtime, replacing the stale import-time constant. The limit is threaded through _sanitize_request_body_for_spend_logs_payload via a new optional parameter. No behavioral changes beyond resolution timing. |
| tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py | Adds a regression test that sets MAX_STRING_LENGTH_PROMPT_IN_DB via monkeypatch.setenv after module import, verifying that the sanitization function honors the runtime env override. Also removes a trailing blank line at end of file. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["_sanitize_request_body_for_spend_logs_payload()"] -->|max_string_length is None| B["_get_max_string_length_prompt_in_db()"]
B --> C{"os.getenv('MAX_STRING_LENGTH_PROMPT_IN_DB')"}
C -->|Set & valid int| D["Use runtime env value"]
C -->|Not set| E["Use DEFAULT_MAX_STRING_LENGTH_PROMPT_IN_DB from constants"]
C -->|Invalid value| E
D --> F["Sanitize strings using resolved limit"]
E --> F
F -->|Nested dict| G["Recurse with same limit (no re-lookup)"]
G --> F
Last reviewed commit: 078be19
| Resolve prompt truncation threshold at runtime so values loaded later via | ||
| proxy config environment_variables are honored. | ||
| """ | ||
| default_max = 2048 |
There was a problem hiding this comment.
Hardcoded default duplicates constant
The default value 2048 is duplicated here and in litellm/constants.py:348 (MAX_STRING_LENGTH_PROMPT_IN_DB = int(os.getenv("MAX_STRING_LENGTH_PROMPT_IN_DB", 2048))). If the default ever changes in constants.py, this location could silently drift out of sync.
Consider importing the constant as the fallback default instead of hardcoding it:
| default_max = 2048 | |
| default_max = 2048 # keep in sync with litellm.constants.MAX_STRING_LENGTH_PROMPT_IN_DB |
Alternatively, you could import the constant and use it as the fallback (since by the time this function is called — not imported — the constant is already resolved from the env at module load time of constants.py):
from litellm.constants import MAX_STRING_LENGTH_PROMPT_IN_DB as _DEFAULT_MAX_STRING_LENGTH
def _get_max_string_length_prompt_in_db() -> int:
max_length_str = os.getenv("MAX_STRING_LENGTH_PROMPT_IN_DB")
if max_length_str is None:
return _DEFAULT_MAX_STRING_LENGTH
...This is a minor style nit — the current approach is functionally correct.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
🚅 Shin's PR Review1. Does this PR fix the issue it describes? 2. Has this issue already been solved elsewhere? 3. Are there other PRs addressing the same problem? 4. Are there other issues this potentially closes? ✅ Looks good — straightforward fix with tests. |
|
@greptileai review |
0ba75bf to
43ffbd7
Compare
|
@greptileai review |
| ), "max_retries should be None when not provided" | ||
|
|
||
|
|
||
| def test_get_request_duration_ms_normal(): |
There was a problem hiding this comment.
Missing blank line between functions
The two blank lines separating test_get_logging_payload_handles_missing_retry_info_gracefully and test_get_request_duration_ms_normal were removed, violating PEP 8's two-blank-lines-between-top-level-definitions convention. This appears to be an accidental whitespace change.
| ), "max_retries should be None when not provided" | |
| def test_get_request_duration_ms_normal(): | |
| ), "max_retries should be None when not provided" | |
| def test_get_request_duration_ms_normal(): |
|
@greptileai review |
1 similar comment
|
@greptileai review |
|
Hi, @krrishdholakia @ishaan-jaff the Greptile review passed with a 5/5 (safe to merge) and ready for maintainer review whenever you have a moment! Fixes #22088. |
f3e31bc
into
BerriAI:litellm_oss_staging_02_26_2026
…#22106) * fix(proxy): honor MAX_STRING_LENGTH_PROMPT_IN_DB from config env vars * fix(proxy): reuse constants fallback for MAX_STRING_LENGTH_PROMPT_IN_DB runtime resolver * test(proxy): restore PEP8 spacing between spend tracking tests
…BerriAI#22106) * fix(proxy): honor MAX_STRING_LENGTH_PROMPT_IN_DB from config env vars * fix(proxy): reuse constants fallback for MAX_STRING_LENGTH_PROMPT_IN_DB runtime resolver * test(proxy): restore PEP8 spacing between spend tracking tests
Fixes #22088
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewCI (LiteLLM team)
Branch creation CI run
Link:
<add link>CI run for the last commit
Link:
<add link>Merge / cherry-pick CI run
Links:
<add link(s)>Type
🐛 Bug Fix
✅ Test
Changes
Issue #22088 root cause was import-time evaluation of
MAX_STRING_LENGTH_PROMPT_IN_DBin spend-tracking sanitization paths. Proxy configenvironment_variablesare loaded later via_load_environment_variables(), so the imported value could become stale.litellm/proxy/spend_tracking/spend_tracking_utils.pyos.environ.litellm.constantsdefault constant when env is missing or invalid._sanitize_request_body_for_spend_logs_payload()resolves the limit once per call and threads it through recursive calls.tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.pyMAX_STRING_LENGTH_PROMPT_IN_DBafter import path setup.tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py-> 32 passedtests/test_litellm/proxy/test_proxy_server.py -k load_environment_variables-> 2 passed, 84 deselected