Skip to content

fix: use ISO timestamps for tool registry Prisma writes - #23732

Closed
milan-berri wants to merge 3 commits into
BerriAI:litellm_oss_staging_03_19_2026from
milan-berri:fix/tool-registry-timestamp-mismatch
Closed

fix: use ISO timestamps for tool registry Prisma writes#23732
milan-berri wants to merge 3 commits into
BerriAI:litellm_oss_staging_03_19_2026from
milan-berri:fix/tool-registry-timestamp-mismatch

Conversation

@milan-berri

@milan-berri milan-berri commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Relevant issues

The global tool registry and tool policy wiring were introduced in:

These changes started passing timezone-aware datetime objects through Prisma into LiteLLM_ToolTable, which surfaces as the Postgres type mismatch described in #23585.

This PR completes and generalizes the partial fix proposed in #23586 by applying the ISO conversion consistently to both batch_upsert_tools and update_tool_policy.

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🐛 Bug Fix

@vercel

vercel Bot commented Mar 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Mar 20, 2026 4:49pm

Request Review

@greptile-apps

greptile-apps Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a Postgres type-mismatch error (#23585) by converting timezone-aware datetime objects to ISO 8601 strings (via .isoformat()) before passing them into Prisma writes for LiteLLM_ToolTable. The two-line change is applied consistently to both affected write paths — batch_upsert_tools and update_tool_policy.

Key changes:

  • litellm/proxy/db/tool_registry_writer.pydatetime.now(timezone.utc) replaced with datetime.now(timezone.utc).isoformat() at lines 86 and 174, ensuring all timestamp fields (last_used_at, created_at, updated_at) written to Prisma are strings.
  • tests/test_litellm/proxy/db/test_tool_registry_writer.py — Explicit isinstance(..., str) assertions added in test_batch_upsert_tools_calls_upsert and test_update_tool_policy_calls_upsert_then_get_tool to verify the fix and act as regression guards.
  • The _mock_row helper intentionally retains datetime objects for its defaults — these simulate values returned from Prisma on the read path and are unaffected by the write-path fix.

Confidence Score: 5/5

  • This PR is safe to merge — the fix is minimal, targeted, and fully covered by mock-only unit tests.
  • Two one-line changes each convert a datetime to an ISO string. The root cause (passing timezone-aware datetime objects directly to Prisma) is well-understood, the fix is applied in both affected code paths, and new isinstance(..., str) assertions verify the correction without introducing real network calls. No other code paths are touched and no backwards-incompatible changes are made.
  • No files require special attention.

Important Files Changed

Filename Overview
litellm/proxy/db/tool_registry_writer.py Two one-line changes convert datetime.now(timezone.utc) to .isoformat() at lines 86 and 174, eliminating the Postgres type-mismatch when timezone-aware datetime objects were passed directly into Prisma writes for LiteLLM_ToolTable. Fix is minimal, correct, and covers all affected write paths.
tests/test_litellm/proxy/db/test_tool_registry_writer.py Adds isinstance(..., str) assertions in two existing tests to verify that timestamp values written to Prisma are ISO strings, not datetime objects. All tests remain mock-only (no real network calls). The _mock_row helper still uses datetime objects for its defaults, which is correct since those simulate values returned from Prisma (read path), not values sent to Prisma (write path).

Sequence Diagram

sequenceDiagram
    participant Caller
    participant batch_upsert_tools
    participant update_tool_policy
    participant Prisma
    participant Postgres

    Note over batch_upsert_tools,update_tool_policy: now = datetime.now(timezone.utc).isoformat()

    Caller->>batch_upsert_tools: items: List[ToolDiscoveryQueueItem]
    batch_upsert_tools->>Prisma: table.upsert(data={create: {last_used_at: "2026-03-20T10:00:00+00:00"}, update: {updated_at: "...", last_used_at: "..."}})
    Prisma->>Postgres: INSERT ... ON CONFLICT ... (ISO string timestamps)
    Postgres-->>Prisma: OK

    Caller->>update_tool_policy: tool_name, input_policy, output_policy
    update_tool_policy->>Prisma: table.upsert(data={create: {created_at: "...", updated_at: "..."}, update: {updated_at: "..."}})
    Prisma->>Postgres: INSERT ... ON CONFLICT ... (ISO string timestamps)
    Postgres-->>Prisma: OK
    update_tool_policy->>Prisma: find_unique(where={tool_name: ...})
    Prisma-->>update_tool_policy: LiteLLM_ToolTableRow
    update_tool_policy-->>Caller: LiteLLM_ToolTableRow
Loading

Last reviewed commit: "Merge branch 'main' ..."

if not data:
return
now = datetime.now(timezone.utc)
now = datetime.now(timezone.utc).isoformat()

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.

No test asserting ISO string format

The fix is correct, but none of the existing tests in test_tool_registry_writer.py assert that the timestamp values passed to Prisma are actually strings (not datetime objects). For example, test_batch_upsert_tools_calls_upsert checks "updated_at" in call_kw["data"]["update"] but not its type.

Adding an explicit assertion would serve as the evidence of resolution called for in the pre-submission checklist (which still shows the testing item unchecked):

assert isinstance(call_kw["data"]["create"]["last_used_at"], str)
assert isinstance(call_kw["data"]["update"]["updated_at"], str)
assert isinstance(call_kw["data"]["update"]["last_used_at"], str)

The same gap exists in test_update_tool_policy_calls_upsert_then_get_tool for created_at and updated_at values at line 174 in update_tool_policy.

Both occurrences of now in this file are affected: line 86 (batch_upsert_tools) and line 174 (update_tool_policy).

Rule Used: What: Ensure that any PR claiming to fix an issue ... (source)

@codspeed-hq

codspeed-hq Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing milan-berri:fix/tool-registry-timestamp-mismatch (51f1d67) with litellm_oss_staging_03_19_2026 (523fbed)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_oss_staging_03_19_2026 (d7c419b) during the generation of this report, so 85ebe89 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Harshit28j Harshit28j left a comment

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.

LGTM

@ghost
ghost changed the base branch from main to litellm_oss_staging_03_19_2026 March 20, 2026 16:48
@ishaan-berri
ishaan-berri deleted the branch BerriAI:litellm_oss_staging_03_19_2026 March 26, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants