Skip to content

Conversation

@nustiueudinastea
Copy link
Contributor

@nustiueudinastea nustiueudinastea commented Dec 5, 2025

DISCLAIMER: this PR was created with the assistance of Claude Code, but I reviewed the small change set. If the project policy is to not accept these please let me know and I will close the PR.

Summary

Fix JSON serialization error when querying metrics that return Decimal values from the dbt Semantic Layer.

What Changed

  • Added from decimal import Decimal import in src/dbt_mcp/semantic_layer/client.py
  • Renamed DateTimeEncoder to ExtendedJSONEncoder and extended it to handle Decimal objects by converting them to float
  • Updated the json.dumps() call to use the renamed encoder class

Why

When querying metrics from the dbt Semantic Layer, PyArrow tables with decimal128/decimal256 columns return Python Decimal objects via table.to_pylist(). The existing DateTimeEncoder only handled datetime and date objects, causing json.dumps() to fail with:

Object of type Decimal is not JSON serializable

MCP response showing the error:

{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Object of type Decimal is not JSON serializable"
      }
    ],
    "isError": false
  }
}

Related Issues

Closes #
Related to #

Checklist

  • I have performed a self-review of my code
  • I have made corresponding changes to the documentation (in https://github.com/dbt-labs/docs.getdbt.com) if required -- No documentation changes required
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Manually verified with a query_metrics call that previously failed:

{
  "limit": 3,
  "metrics": ["sqls"],
  "group_by": [{"name": "metric_time", "grain": "MONTH", "type": "time_dimension"}],
  "order_by": [{"name": "metric_time", "descending": true}]
}

Now returns successfully:

[
  {"metric_time__month": "2025-12-01T00:00:00+00:00", "sqls": 245.0},
  {"metric_time__month": "2025-11-01T00:00:00+00:00", "sqls": 134.0},
  {"metric_time__month": "2025-10-01T00:00:00+00:00", "sqls": 9373.0}
]

PyArrow tables with decimal128/decimal256 columns return Python Decimal
objects when converted via to_pylist(). The standard json.dumps() cannot
serialize Decimal objects, causing "Object of type Decimal is not JSON
serializable" errors when querying metrics.

Extended the JSON encoder to convert Decimal objects to float values.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@nustiueudinastea nustiueudinastea requested review from a team, b-per and jasnonaz as code owners December 5, 2025 11:04
@b-per
Copy link
Collaborator

b-per commented Dec 5, 2025

Thanks @nustiueudinastea !

We don't have any problem with people using AI/Claude as long as there is some human analysis/review in the loop, like you did. I am ok with approving the change, but would you be able to add a changelog entry using changie like described here?

@nustiueudinastea
Copy link
Contributor Author

Sure @b-per , let me do that and get back to you.

@nustiueudinastea
Copy link
Contributor Author

@b-per please take a look. I added the change using changie.

@b-per b-per merged commit dbf22e8 into dbt-labs:main Dec 5, 2025
5 checks passed
@nustiueudinastea nustiueudinastea deleted the fix/decimal-json-serialization branch December 6, 2025 20:17
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.

2 participants