Skip to content

fix: handle numpy scalar types in safe_json_value#35389

Open
avasis-ai wants to merge 1 commit intolanggenius:mainfrom
avasis-ai:fix/numpy-scalar-json-serialization
Open

fix: handle numpy scalar types in safe_json_value#35389
avasis-ai wants to merge 1 commit intolanggenius:mainfrom
avasis-ai:fix/numpy-scalar-json-serialization

Conversation

@avasis-ai
Copy link
Copy Markdown

Summary

Add explicit handling for np.integer and np.floating scalar types in safe_json_value().

Problem

safe_json_value() handles np.ndarray (via .tolist()) but misses numpy scalar types like np.int64, np.float64, etc. These fall through to the else clause and are returned as-is. Since json.dumps() cannot serialize numpy scalar types, this causes a TypeError at runtime when tool results contain numpy scalars.

In numpy >= 2.0, scalar types no longer inherit from Python builtins, making this even more likely to fail.

Change

Added two checks before the existing np.ndarray handling:

  • np.integerint(v)
  • np.floatingfloat(v)

Related to #34405 (parameter precision loss with numeric types).

@avasis-ai avasis-ai requested a review from QuantumGhost as a code owner April 19, 2026 07:21
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 19, 2026
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 19, 2026
@fatelei fatelei added this pull request to the merge queue Apr 19, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 19, 2026
@fatelei
Copy link
Copy Markdown
Contributor

fatelei commented Apr 19, 2026

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================ tests coverage ================================
_______________ coverage: platform linux, python 3.12.3-final-0 ________________

Coverage XML written to file coverage.xml
Coverage JSON written to file coverage.json
=========================== short test summary info ============================
FAILED api/tests/unit_tests/core/mcp/session/test_base_session.py::test_receive_loop_cancel_notification - AttributeError: 'NoneType' object has no attribute 'message'
===== 1 failed, 10659 passed, 4 skipped, 297 warnings in 311.62s (0:05:11) =====

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer needs-revision size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants