numpy.bytes_ attributes are recorded as a truncated repr instead of their content
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- numpy, python
- Domain
- observability
Research direction
The bug is in the bytes encoder of logfire-sdk/logfire/internal/json_encoder.py, which slices repr(o)[2:-1]. Reproduce with the script in the issue using numpy.bytes and a plain bytes value to see the truncated output. Change the encoder to use the bytes base class repr, then check that numpy.bytes_ and plain bytes both record as "hello". Done when a regression test covering a bytes subclass with an overridden repr passes.
Written by the indexing model from the issue text.
Description
Description
A bytes attribute is recorded as its content without the b prefix and quotes. A numpy.bytes_ value (a bytes subclass, e.g. an element of a NumPy array with a bytes dtype) is recorded as a truncated piece of its repr instead, when NumPy 2 is installed.
import numpy
import logfire
from logfire.testing import SimpleSpanProcessor, TestExporter
exporter = TestExporter()
logfire.configure(send_to_logfire=False, console=False, additional_span_processors=[SimpleSpanProcessor(exporter)])
logfire.info('hi', plain=b'hello', np_bytes=numpy.bytes_(b'hello'), from_array=numpy.array([b'hello'])[0])
attributes = exporter.exported_spans[0].attributes
for key in ('plain', 'np_bytes', 'from_array'):
print(f'{key:10} -> {attributes[key]!r}')
Output on main (137643a5):
plain -> '"hello"'
np_bytes -> '".bytes_(b\'hello\'"'
from_array -> '".bytes_(b\'hello\'"'
Expected: all three attributes are recorded as "hello".
The cause is in the bytes encoder in logfire-sdk/logfire/_internal/json_encoder.py, which returns repr(o)[2:-1] to strip the b prefix and the quotes. That slice assumes the repr of plain bytes. NumPy 2 changed the repr of numpy.bytes_ to np.bytes_(b'hello'), so the slice removes the leading np and the closing parenthesis. The same happens for any bytes subclass that overrides its repr.
Calling the repr of the bytes base class directly, instead of repr(o), gives the same result as for plain bytes.
I have a fix with a regression test ready and am opening a PR for it.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
Python, Logfire & OS Versions, related packages (not required)
logfire="6.0.0"
platform="macOS-15.3.1-arm64-arm-64bit"
python="3.12.13 (main, Jun 11 2026, 03:55:21) [Clang 22.1.3 ]"
[related_packages]
numpy="2.5.5"
pydantic="2.14.4"
opentelemetry-api="1.45.2"
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 376
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 124
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydantic/logfire
-
Windows: 16 tests in `tests/test_cli.py` fail, and the CI test job only runs on LinuxMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Docs: update the Playground cost estimate note for Gateway custom pricesPossibly taken @henricop claimed this 20 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Pydantic fields with a custom serializer get the wrong JSON schema, which crashes the verbose console exporterPossibly taken @MaiAditya claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 42/100
Maintainers usually reply within 1 day
-
Custom model_serializer returning a str or list crashes the verbose console exporterPossibly taken @nurlanmnn claimed this 2 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/100
Maintainers usually reply within 1 day
All issues in pydantic/logfire
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
cal-itp/data-infra#5887 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ranacseruet/vaice#139 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
batocera-linux/batocera.linux#17423 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 60/100
5hyuns/maya-workflow-kit-for-blender#440 ·
Maintainers usually reply within 1 day