numpy.bytes_ attributes are recorded as a truncated repr instead of their content

Open Beginner friendly
#2,858 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@JoeyTan21 is already working on this.

Since Nov 21, 2026.

  • #2859 by @JoeyTan21 — open

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/logfire

All issues in pydantic/logfire

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.