Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6b591c3
fix(ovhcloud): migrate reasoning_content->reasoning and duration->sec…
KunalG67 Apr 27, 2026
e55e73d
fix(ovhcloud): use explicit None check for seconds field in STT response
KunalG67 Apr 27, 2026
c0da139
fix(otel): populate gen_ai.output.messages and gen_ai.system_instruct…
Aneesh-Fiddler Apr 27, 2026
4d2e13c
test(otel): add tests for Responses API output messages, system instr…
Aneesh-Fiddler Apr 27, 2026
e70b0c9
style: apply black formatting to opentelemetry.py
Aneesh-Fiddler Apr 28, 2026
c30d58f
fix: resolve mypy indexed assignment error in function_call handling
Aneesh-Fiddler Apr 28, 2026
466b4dd
fix: address review comments — Pydantic compat, falsy fallthrough, pe…
Aneesh-Fiddler Apr 28, 2026
982fed4
fix(ovhcloud): handle reasoning field migration in non-streaming resp…
KunalG67 Apr 28, 2026
8f48d88
style: apply black formatting to ovhcloud chat transformation
KunalG67 Apr 28, 2026
9928618
fix: remove duplicate gen_ai.response.finish_reasons block
Aneesh-Fiddler Apr 28, 2026
90bcd23
fix(ovhcloud): remove dead transform_response override
KunalG67 Apr 28, 2026
d73e24c
fix(ovhcloud): remove dead transform_response override, parent alread…
KunalG67 Apr 28, 2026
5310252
ci: retrigger checks after retargeting to litellm_oss_staging_04_27_2026
Aneesh-Fiddler Apr 29, 2026
c319a19
fix: handle raw Pydantic v2 models from openai SDK in output transfor…
Aneesh-Fiddler Apr 30, 2026
209bd0b
fix(proxy): sort spend updates to prevent DB deadlocks
pnookala-godaddy May 5, 2026
2993e45
allow non-admin roles on /compliance/* read routes
May 5, 2026
85d4d96
fix(proxy): preserve HTTP operations when injecting WebSocket stubs i…
michelligabriele May 5, 2026
ed7ded9
Merge PR #27221 into agent staging branch
May 6, 2026
062b5b3
Add main module header comment
cursoragent May 6, 2026
b631863
Add utils module docstring
oss-agent-shin May 6, 2026
f8c0e03
Merge PR #27255 into agent staging branch
May 6, 2026
5b91fc0
Merge PR #27253 into agent staging branch
May 6, 2026
1087766
Merge PR #27244 into agent staging branch
May 6, 2026
abf0381
Merge PR #27234 into agent staging branch
May 6, 2026
75245de
Merge PR #26595 into agent staging branch
May 6, 2026
a466c2e
Merge PR #26670 into agent staging branch
May 6, 2026
ce17e94
Merge branch 'litellm_internal_staging' into litellm_agent_oss_stagin…
Sameerlite May 11, 2026
99218c6
Fix deprecated model test
Sameerlite May 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .evidence/main_header_repro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions litellm/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# LiteLLM main module: public completion, embedding, streaming, and moderation entrypoints.
#
# +-----------------------------------------------+
# | |
# | Give Feedback / Get Help |
Expand Down
48 changes: 27 additions & 21 deletions litellm/proxy/db/db_spend_update_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,10 +1131,12 @@ async def _commit_spend_updates_to_db( # noqa: PLR0915
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for (
user_id,
response_cost,
) in user_list_transactions.items():
# Sort by ID for consistent lock ordering across pods to prevent deadlocks.
# batch_() issues statements sequentially within the tx, so iteration
# order = lock acquisition order.
for user_id, response_cost in sorted(
user_list_transactions.items()
):
batcher.litellm_usertable.update_many(
where={"user_id": user_id},
data={"spend": {"increment": response_cost}},
Expand Down Expand Up @@ -1186,10 +1188,10 @@ async def _commit_spend_updates_to_db( # noqa: PLR0915
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for (
token,
response_cost,
) in key_list_transactions.items():
# Sort by token for consistent lock ordering across pods to prevent deadlocks.
for token, response_cost in sorted(
key_list_transactions.items()
):
batcher.litellm_verificationtoken.update_many( # 'update_many' prevents error from being raised if no row exists
where={"token": token},
data={
Expand Down Expand Up @@ -1230,10 +1232,10 @@ async def _commit_spend_updates_to_db( # noqa: PLR0915
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for (
team_id,
response_cost,
) in team_list_transactions.items():
# Sort by team_id for consistent lock ordering across pods to prevent deadlocks.
for team_id, response_cost in sorted(
team_list_transactions.items()
):
verbose_proxy_logger.debug(
"Updating spend for team id={} by {}".format(
team_id, response_cost
Expand Down Expand Up @@ -1288,10 +1290,11 @@ async def _commit_spend_updates_to_db( # noqa: PLR0915
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for (
key,
response_cost,
) in team_member_list_transactions.items():
# Sort by composite key for consistent lock ordering across pods to prevent deadlocks.
# Key format "team_id::<v>::user_id::<v>" makes the string sort equivalent to sorting by (team_id, user_id).
for key, response_cost in sorted(
team_member_list_transactions.items()
):
# key is "team_id::<value>::user_id::<value>"
team_id = key.split("::")[1]
user_id = key.split("::")[3]
Expand Down Expand Up @@ -1348,10 +1351,10 @@ async def _commit_spend_updates_to_db( # noqa: PLR0915
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for (
org_id,
response_cost,
) in org_list_transactions.items():
# Sort by org_id for consistent lock ordering across pods to prevent deadlocks.
for org_id, response_cost in sorted(
org_list_transactions.items()
):
batcher.litellm_organizationtable.update_many( # 'update_many' prevents error from being raised if no row exists
where={"organization_id": org_id},
data={"spend": {"increment": response_cost}},
Expand Down Expand Up @@ -1439,7 +1442,10 @@ async def _update_entity_spend_in_db(
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for entity_id, response_cost in transactions.items():
# Sort by entity_id for consistent lock ordering across pods to prevent deadlocks.
for entity_id, response_cost in sorted(
transactions.items()
):
verbose_proxy_logger.debug(
f"Updating spend for {entity_name} {where_field}={entity_id} by {response_cost}"
)
Expand Down
8 changes: 4 additions & 4 deletions litellm/proxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4879,10 +4879,10 @@ async def update_end_user_spend(
timeout=timedelta(seconds=60)
) as transaction:
async with transaction.batch_() as batcher:
for (
end_user_id,
response_cost,
) in end_user_list_transactions.items():
# Sort by end_user_id for consistent lock ordering across pods to prevent deadlocks.
for end_user_id, response_cost in sorted(
end_user_list_transactions.items()
):
if litellm.max_end_user_budget is not None:
pass
batcher.litellm_endusertable.upsert(
Expand Down
2 changes: 2 additions & 0 deletions litellm/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Utility helpers for LiteLLM core request handling and provider support."""

# from __future__ import annotations must be the first non-comment statement
from __future__ import annotations
Comment on lines 3 to 4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The comment is now inaccurate. The module docstring added by this PR is itself an expression statement (an Expr node in Python's AST), so from __future__ import annotations is no longer the first non-comment statement. PEP 236 explicitly permits module docstrings before future imports, so behaviour is unchanged — but the inline explanation is misleading for anyone reading it in future.

Suggested change
# from __future__ import annotations must be the first non-comment statement
from __future__ import annotations
# from __future__ import annotations must appear before any other imports/code
# (module docstring above is permitted by PEP 236)
from __future__ import annotations


Expand Down
143 changes: 143 additions & 0 deletions tests/test_litellm/proxy/db/test_db_spend_update_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,3 +1513,146 @@ async def test_commit_spend_updates_uses_pipeline():
mock_redis_update_buffer.get_all_daily_end_user_spend_update_transactions_from_redis_buffer.assert_not_called()
mock_redis_update_buffer.get_all_daily_agent_spend_update_transactions_from_redis_buffer.assert_not_called()
mock_redis_update_buffer.get_all_daily_tag_spend_update_transactions_from_redis_buffer.assert_not_called()


@pytest.mark.parametrize(
"bucket_name,input_dict,table_attr,method_name,where_key,expected_order",
[
pytest.param(
"user_list_transactions",
{"user_c": 0.1, "user_a": 0.2, "user_b": 0.3},
"litellm_usertable",
"update_many",
"user_id",
["user_a", "user_b", "user_c"],
id="user",
),
pytest.param(
"key_list_transactions",
{"tok_c": 0.1, "tok_a": 0.2, "tok_b": 0.3},
"litellm_verificationtoken",
"update_many",
"token",
["tok_a", "tok_b", "tok_c"],
id="key",
),
pytest.param(
"team_list_transactions",
{"team_c": 0.1, "team_a": 0.2, "team_b": 0.3},
"litellm_teamtable",
"update_many",
"team_id",
["team_a", "team_b", "team_c"],
id="team",
),
pytest.param(
"team_member_list_transactions",
{
"team_id::team_c::user_id::user_x": 0.1,
"team_id::team_a::user_id::user_x": 0.2,
"team_id::team_b::user_id::user_x": 0.3,
},
"litellm_teammembership",
"update_many",
"team_id",
["team_a", "team_b", "team_c"],
id="team_member",
),
pytest.param(
"org_list_transactions",
{"org_c": 0.1, "org_a": 0.2, "org_b": 0.3},
"litellm_organizationtable",
"update_many",
"organization_id",
["org_a", "org_b", "org_c"],
id="org",
),
pytest.param(
"end_user_list_transactions",
{"eu_c": 0.1, "eu_a": 0.2, "eu_b": 0.3},
"litellm_endusertable",
"upsert",
"user_id",
["eu_a", "eu_b", "eu_c"],
id="end_user",
),
pytest.param(
"tag_list_transactions",
{"prod": 0.1, "customer-x": 0.2, "test": 0.3},
"litellm_tagtable",
"update_many",
"tag_name",
["customer-x", "prod", "test"],
id="tag",
),
pytest.param(
"agent_list_transactions",
{"agent_c": 0.1, "agent_a": 0.2, "agent_b": 0.3},
"litellm_agentstable",
"update_many",
"agent_id",
["agent_a", "agent_b", "agent_c"],
id="agent",
),
],
)
@pytest.mark.asyncio
async def test_commit_spend_updates_iterates_in_sorted_order(
bucket_name, input_dict, table_attr, method_name, where_key, expected_order
):
"""
Every spend-bucket code path in _commit_spend_updates_to_db must iterate
in sorted order so concurrent pods acquire row locks in the same order
and avoid PostgreSQL deadlocks. Covers the 5 direct loops (user/key/team/
team_member/org), the end_user path in ProxyUpdateSpend.update_end_user_spend,
and the shared _update_entity_spend_in_db helper (tag, agent).
"""
db_writer = DBSpendUpdateWriter()

captured_where_values = []

def capture(*, where, data):
captured_where_values.append(where[where_key])

mock_batcher = MagicMock()
table_mock = MagicMock()
setattr(table_mock, method_name, MagicMock(side_effect=capture))
setattr(mock_batcher, table_attr, table_mock)

mock_transaction = AsyncMock()
mock_transaction.__aenter__ = AsyncMock(return_value=mock_transaction)
mock_transaction.__aexit__ = AsyncMock(return_value=False)
mock_transaction.batch_ = MagicMock(
return_value=AsyncMock(
__aenter__=AsyncMock(return_value=mock_batcher),
__aexit__=AsyncMock(return_value=False),
)
)

mock_prisma_client = MagicMock()
mock_prisma_client.db = MagicMock()
mock_prisma_client.db.tx = MagicMock(return_value=mock_transaction)

mock_proxy_logging = MagicMock()
mock_proxy_logging.call_details = {}

buckets = {
"user_list_transactions": {},
"end_user_list_transactions": {},
"key_list_transactions": {},
"team_list_transactions": {},
"team_member_list_transactions": {},
"org_list_transactions": {},
"tag_list_transactions": {},
"agent_list_transactions": {},
}
buckets[bucket_name] = input_dict

await db_writer._commit_spend_updates_to_db(
prisma_client=mock_prisma_client,
n_retry_times=3,
proxy_logging_obj=mock_proxy_logging,
db_spend_update_transactions=buckets,
)

assert captured_where_values == expected_order
13 changes: 13 additions & 0 deletions tests/test_litellm/test_main_module_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pathlib import Path


def test_main_py_starts_with_brief_file_description():
repo_root = Path(__file__).resolve().parents[2]
main_py = repo_root / "litellm" / "main.py"

first_two_lines = main_py.read_text(encoding="utf-8").splitlines()[:2]

assert any(
"LiteLLM main module" in line and "entrypoints" in line
for line in first_two_lines
)
11 changes: 11 additions & 0 deletions tests/test_litellm/test_utils_module_docstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ast
from pathlib import Path


def test_utils_module_has_docstring():
utils_path = Path(__file__).parents[2] / "litellm" / "utils.py"
module = ast.parse(utils_path.read_text())

assert ast.get_docstring(module) == (
"Utility helpers for LiteLLM core request handling and provider support."
)
Loading