Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on bad json. #902

Open
vincentdavis opened this issue Mar 3, 2025 · 9 comments
Open

Crash on bad json. #902

vincentdavis opened this issue Mar 3, 2025 · 9 comments

Comments

@vincentdavis
Copy link

Description

I am migrating data between two postgres db's one has native JSON field the other is using Djangos JSON fields.
I am getting errors related to converting /U0000.
I tried to log the json with logfire and logfire crashed.

  • "logfire[asgi,django,psycopg]>=3.5.3",
    
  • "pydantic>=2.10.6",
    
[2025-03-03 14:17:34 +0000] [76] [INFO] Booting worker with pid: 76

    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/logfire/_internal/main.py", line 355, in info

    self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)

    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/logfire/_internal/main.py", line 679, in log

    msg, extra_attrs, msg_template = logfire_format_with_magic(

                                     ~~~~~~~~~~~~~~~~~~~~~~~~~^

        msg_template,

        ^^^^^^^^^^^^^

    ...<2 lines>...

        fstring_frame=fstring_frame,

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    )

    ^

  File "/app/.venv/lib/python3.13/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic

    chunks, extra_attrs, new_template = chunks_formatter.chunks(

                                        ~~~~~~~~~~~~~~~~~~~~~~~^

        format_string,

        ^^^^^^^^^^^^^^

    ...<2 lines>...

        fstring_frame=fstring_frame,

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    )

Logfire project URL: https://logfire.pydantic.dev/vincentdavis/app-gotta-bike

Python, Logfire & OS Versions, related packages (not required)

logfire="3.6.2"
platform="macOS-15.3.1-arm64-arm-64bit"
python="3.12.4 (v3.12.4:8e8a4baf65, Jun  6 2024, 17:33:18) [Clang 13.0.0 (clang-1300.0.29.30)]"
[related_packages]
requests="2.32.3"
pydantic="2.10.6"
protobuf="5.29.3"
rich="13.9.4"
executing="2.2.0"
opentelemetry-api="1.30.0"
opentelemetry-exporter-otlp-proto-common="1.30.0"
opentelemetry-exporter-otlp-proto-http="1.30.0"
opentelemetry-instrumentation="0.51b0"
opentelemetry-instrumentation-asgi="0.51b0"
opentelemetry-instrumentation-dbapi="0.51b0"
opentelemetry-instrumentation-django="0.51b0"
opentelemetry-instrumentation-psycopg="0.51b0"
opentelemetry-instrumentation-wsgi="0.51b0"
opentelemetry-proto="1.30.0"
opentelemetry-sdk="1.30.0"
opentelemetry-semantic-conventions="0.51b0"
opentelemetry-util-http="0.51b0"
@alexmojaki
Copy link
Contributor

Please share the full traceback and error message

@vincentdavis
Copy link
Author

It was running in a container, this is all I have. Logfire did not log the error because it crashed. I add a bit before what I sent earlier.

  File "/app/.venv/lib/python3.13/site-packages/django/utils/deprecation.py", line 129, in __call__

    response = response or self.get_response(request)

                           ~~~~~~~~~~~~~~~~~^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner

    response = get_response(request)

  File "/app/.venv/lib/python3.13/site-packages/whitenoise/middleware.py", line 123, in __call__

    return self.get_response(request)

           ~~~~~~~~~~~~~~~~~^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner

    response = get_response(request)

  File "/app/.venv/lib/python3.13/site-packages/django/core/handlers/base.py", line 197, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

  File "/usr/local/lib/python3.13/contextlib.py", line 85, in inner

    return func(*args, **kwds)

  File "/app/.venv/lib/python3.13/site-packages/django/views/generic/base.py", line 104, in view

    return self.dispatch(request, *args, **kwargs)

           ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/django/utils/decorators.py", line 48, in _wrapper

    return bound_method(*args, **kwargs)

  File "/app/.venv/lib/python3.13/site-packages/django/contrib/auth/decorators.py", line 60, in _view_wrapper

    return view_func(request, *args, **kwargs)

  File "/app/.venv/lib/python3.13/site-packages/django/views/generic/base.py", line 143, in dispatch

    return handler(request, *args, **kwargs)

  File "/app/apps/usac/views.py", line 139, in post

    api_data=sanitize_json(race_id, row["api_data"]),

             ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/app/apps/usac/views.py", line 31, in sanitize_json

    logfire.info(f"JSON data: {data}")

14:17:33.088   JSON data: {'error': False, 'message': '<div class=\'table\'><div class=\...results\'>GPCX on the DL</div>\n                </div></div>'}

14:17:33.100   JSON data: [{'place': '1', 'points': '380.53', 'name': 'Collin Hudson', '...ac_number': '279043', 'bib': '58', 'team': 'Brevard College'}]

14:17:33.125   JSON data: {'error': False, 'message': "<div class='table'><div class='ta...results'>Brevard College</div>\n                </div></div>"}

[2025-03-03 14:17:34 +0000] [76] [INFO] Booting worker with pid: 76

    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/logfire/_internal/main.py", line 355, in info

    self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)

    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/app/.venv/lib/python3.13/site-packages/logfire/_internal/main.py", line 679, in log

    msg, extra_attrs, msg_template = logfire_format_with_magic(

                                     ~~~~~~~~~~~~~~~~~~~~~~~~~^

        msg_template,

        ^^^^^^^^^^^^^

    ...<2 lines>...

        fstring_frame=fstring_frame,

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    )

    ^

  File "/app/.venv/lib/python3.13/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic

    chunks, extra_attrs, new_template = chunks_formatter.chunks(

                                        ~~~~~~~~~~~~~~~~~~~~~~~^

        format_string,

        ^^^^^^^^^^^^^^

    ...<2 lines>...

        fstring_frame=fstring_frame,

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    )

Logfire project URL: https://logfire.pydantic.dev/vincentdavis/app-gotta-bike

Stopping Container

@vincentdavis
Copy link
Author

Got a bit more detail. I have attached the offending json data. The issue is around "Kevin 'Brett\u0000 Stakelin"
The code below would crash logfire when it logged the data logfire.info(f"JSON data: {data}")

def sanitize_json(race_id, data):
    if data is None:
        return None
    try:
        if isinstance(data, str):
            # For string representation of JSON
            string = data.replace("\u0000", "")
            return string
        elif isinstance(data, (dict, list)):
            logfire.info(f"JSON data: {data}")
            # For Python objects, convert to JSON string, sanitize, then parse back
            json_str = json.dumps(data)
            sanitized_str = json_str.replace("\u0000", "")
            json_data = json.loads(sanitized_str)
            return json_data
        else:
            logfire.warning(f"** Non-string data or dict: {data}")
            return data
    except Exception as e:
        logfire.exception(f"Error: {race_id}, sanitizing JSON data: {e!s}")
        return None
{'json_str': '[{"place": "1", "points": "245.29", "name": "Chris Hamilton", "city_state": "Denver, CO", "time": "44:03.1", "usac_number": "307709", "bib": "", "team": ""}, {"place": "2", "points": "258.67", "name": "Shawn Kingrey", "city_state": "Pueblo, CO", "time": "44:06.6", "usac_number": "167284", "bib": "", "team": ""}, {"place": "3", "points": "272.05", "name": "Bill Bottom", "city_state": "Fort Collins, CO", "time": "44:31.1", "usac_number": "363282", "bib": "", "team": ""}, {"place": "4", "points": "285.43", "name": "Kyle Link", "city_state": "Steamboat Springs, CO", "time": "44:31.1", "usac_number": "517925", "bib": "", "team": ""}, {"place": "5", "points": "298.81", "name": "Jay Paidipati", "city_state": "Superior, CO", "time": "44:41.0", "usac_number": "484498", "bib": "", "team": ""}, {"place": "6", "points": "312.19", "name": "Neal Mendel", "city_state": "Denver, CO", "time": "45:10.2", "usac_number": "286615", "bib": "", "team": ""}, {"place": "7", "points": "325.57", "name": "Robert Vester", "city_state": "Milwaukee, WI", "time": "45:28.7", "usac_number": "448238", "bib": "", "team": ""}, {"place": "8", "points": "338.95", "name": "Brent Brown", "city_state": "Denver, CO", "time": "46:12.4", "usac_number": "438302", "bib": "", "team": ""}, {"place": "9", "points": "352.33", "name": "Mike Higgins", "city_state": "Canon City, CO", "time": "46:47.4", "usac_number": "88299", "bib": "", "team": ""}, {"place": "10", "points": "365.71", "name": "Eric Falldin", "city_state": "Berthoud, CO", "time": "47:18.0", "usac_number": "243927", "bib": "", "team": ""}, {"place": "11", "points": "379.09", "name": "Kevin \'Brett\\u0000 Stakelin", "city_state": "Denver, CO", "time": "49:22.5", "usac_number": "282065", "bib": "", "team": ""}, {"place": "12", "points": "392.47", "name": "Dave DuBois", "city_state": "Fort Collins", "time": "50:45.3", "usac_number": "416557", "bib": "", "team": ""}, {"place": "13", "points": "405.85", "name": "Allan Marvin", "city_state": "Denver, CO", "time": "50:45.7", "usac_number": "22787", "bib": "", "team": ""}, {"place": "14", "points": "419.23", "name": "Cris Compton", "city_state": "Littleton", "time": "51:22.6", "usac_number": "378190", "bib": "", "team": ""}]', 'race_id': 1043765}

@vincentdavis
Copy link
Author

One thing that is strange is I have no problem migrating to a sqlitedb, this is only a problem from postgres to postrges

@alexmojaki
Copy link
Contributor

I'm not managing to reproduce a problem. This works fine:

import json

import logfire

logfire.configure()
json_str = '[{"place": "1", "points": "245.29", "name": "Chris Hamilton", "city_state": "Denver, CO", "time": "44:03.1", "usac_number": "307709", "bib": "", "team": ""}, {"place": "2", "points": "258.67", "name": "Shawn Kingrey", "city_state": "Pueblo, CO", "time": "44:06.6", "usac_number": "167284", "bib": "", "team": ""}, {"place": "3", "points": "272.05", "name": "Bill Bottom", "city_state": "Fort Collins, CO", "time": "44:31.1", "usac_number": "363282", "bib": "", "team": ""}, {"place": "4", "points": "285.43", "name": "Kyle Link", "city_state": "Steamboat Springs, CO", "time": "44:31.1", "usac_number": "517925", "bib": "", "team": ""}, {"place": "5", "points": "298.81", "name": "Jay Paidipati", "city_state": "Superior, CO", "time": "44:41.0", "usac_number": "484498", "bib": "", "team": ""}, {"place": "6", "points": "312.19", "name": "Neal Mendel", "city_state": "Denver, CO", "time": "45:10.2", "usac_number": "286615", "bib": "", "team": ""}, {"place": "7", "points": "325.57", "name": "Robert Vester", "city_state": "Milwaukee, WI", "time": "45:28.7", "usac_number": "448238", "bib": "", "team": ""}, {"place": "8", "points": "338.95", "name": "Brent Brown", "city_state": "Denver, CO", "time": "46:12.4", "usac_number": "438302", "bib": "", "team": ""}, {"place": "9", "points": "352.33", "name": "Mike Higgins", "city_state": "Canon City, CO", "time": "46:47.4", "usac_number": "88299", "bib": "", "team": ""}, {"place": "10", "points": "365.71", "name": "Eric Falldin", "city_state": "Berthoud, CO", "time": "47:18.0", "usac_number": "243927", "bib": "", "team": ""}, {"place": "11", "points": "379.09", "name": "Kevin \'Brett\\u0000 Stakelin", "city_state": "Denver, CO", "time": "49:22.5", "usac_number": "282065", "bib": "", "team": ""}, {"place": "12", "points": "392.47", "name": "Dave DuBois", "city_state": "Fort Collins", "time": "50:45.3", "usac_number": "416557", "bib": "", "team": ""}, {"place": "13", "points": "405.85", "name": "Allan Marvin", "city_state": "Denver, CO", "time": "50:45.7", "usac_number": "22787", "bib": "", "team": ""}, {"place": "14", "points": "419.23", "name": "Cris Compton", "city_state": "Littleton", "time": "51:22.6", "usac_number": "378190", "bib": "", "team": ""}]'

data = json.loads(json_str)
logfire.info(f'JSON data: {data}')

@vincentdavis
Copy link
Author

import json
import logfire
data = {"name": "Kevin \'Brett\u0000 Stakelin"}
logfire.info(f'JSON data: {data}')
/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py:355: InspectArgumentsFailedWarning: Failed to introspect calling code. Please report this issue to Logfire. Falling back to normal message formatting which may result in loss of information if using an f-string. Set inspect_arguments=False in logfire.configure() to suppress this warning. The problem was:
No source code available. This happens when running in an interactive shell, using exec(), or running .pyc files without the source .py files.
  self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)
/Users/vincentdavis/Applications/PyCharm Professional Edition.app/Contents/plugins/python-ce/helpers/pydev/pydevconsole.py:364: FormattingFailedWarning: 
    Ensure you are either:
      (1) passing an f-string directly, with inspect_arguments enabled and working, or
      (2) passing a literal `str.format`-style template, not a preformatted string.
    See https://logfire.pydantic.dev/docs/guides/onboarding-checklist/add-manual-tracing/#messages-and-span-names.
    The problem was: The field {'name'} is not defined.
  coro = func()
15:54:30.967 Failed to introspect calling code. Please report this issue to Logfire. Falling back to normal message formatting which may result in loss of information if using an f-string. Set inspect_arguments=False in logfire.configure() to suppress this warning. The problem was:
No source code available. This happens when running in an interactive shell, using exec(), or running .pyc files without the source .py files.
15:54:30.968 JSON data: {'name': "Kevin 'Brett\x00 Stakelin"}

@alexmojaki
Copy link
Contributor

That's a different problem. I'm guessing you ran that code in an interactive shell/console?

@vincentdavis
Copy link
Author

I can reproduce now.
More details.
I am going data between a postgred db's from a json field to a jsonb field. jsonb does not allow null values.
Depending on how you view the data you see different things.

{"name": "Kevin \'Brett\\u0000 Stakelin"}
{"name": "Kevin 'Brett\u0000 Stakelin"}
{"name": "Kevin 'Brett\x0000 Stakelin"}

I can't make the test file in Pycharm orm as it will remove the escape characters causing the problem. I have attached the file and a screenshot of the file in case your editor "fixes the file"
I get the error with the test line and also loading from the database.

import os

import logfire
import psycopg
from dotenv import load_dotenv
from psycopg.rows import dict_row

load_dotenv()

logfire.configure()
data_1 = {"name": "Kevin \'Brett\\u0000 Stakelin"}
logfire.info(f"JSON data_1: {data_1}")
logfire.info(data_1)
print(data_1)
print("----")


data_2 = {"name": "Kevin 'Brett\\u0000 Stakelin"}
logfire.info(f"JSON data_2: {data_2}\n")
logfire.info(data_2)
print(data_2)
print("----")

data_3 = {"name": "Kevin 'Brett\u0000 Stakelin"}
logfire.info(f"JSON data_3: {data_3}\n")
logfire.info(data_3)
print(data_3)
print("----")

old_db = psycopg.connect(
    dbname="railway",
    user="postgres",
    password=os.getenv("USACLEGACY_PASSWORD"),
    host=os.getenv("USACLEGACY_HOST"),
    port=os.getenv("USACLEGACY_PORT"),
    connect_timeout=30,  # Increase connection timeout
)
old_cursor = old_db.cursor(row_factory=dict_row)
query = """
                    SELECT error, name, race_id, year, number, load_info_id,
                           start_date, last_updated, results, api_data,
                           date_created, date_modified 
                    FROM race_id
                    WHERE race_id = 1043765
                """
old_cursor.execute(query)
rows = old_cursor.fetchall()
for row in rows:
    logfire.info(f"Row: {row}")
    logfire.info(f"JSON data: {row['results']}")
    logfire.info(row["results"])
    # print(row["results"])
Image
(.venv)(base) ~/DEV/REPO/_GOTTS.BIKE/app_gotta_bike git:[vmd_dev]
uv run test_pydantic.py
17:02:35.646 JSON data_1: {'name': "Kevin 'Brett\\u0000 Stakelin"}
Caught an internal error in Logfire. Your code should still be running fine, just with less telemetry. This is just logging the internal error.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/test_pydantic.py", line 13, in <module>
    logfire.info(data_1)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 355, in info
    self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 679, in log
    msg, extra_attrs, msg_template = logfire_format_with_magic(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic
    chunks, extra_attrs, new_template = chunks_formatter.chunks(
                                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 55, in chunks
    chunks, extra_attrs = self._vformat_chunks(
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 228, in _vformat_chunks
    for literal_text, field_name, format_spec, conversion in self.parse(format_string):
                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/string.py", line 288, in parse
    return _string.formatter_parser(format_string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, got dict
Invalid type dict for attribute 'logfire.msg_template' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Invalid type dict for attribute 'logfire.msg' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Exception while exporting Span.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 113, in on_end
    self.span_exporter.export((span,))
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 92, in export
    self._log_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 371, in _log_span
    self._print_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 122, in _print_span
    self._console.print(Text.assemble(*parts))
                        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 397, in assemble
    append(*part)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 979, in append
    raise TypeError("Only str or Text can be appended to Text")
TypeError: Only str or Text can be appended to Text
{'name': "Kevin 'Brett\\u0000 Stakelin"}
----
17:02:35.652 JSON data_2: {'name': "Kevin 'Brett\\u0000 Stakelin"}

Caught an internal error in Logfire. Your code should still be running fine, just with less telemetry. This is just logging the internal error.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/test_pydantic.py", line 20, in <module>
    logfire.info(data_2)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 355, in info
    self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 679, in log
    msg, extra_attrs, msg_template = logfire_format_with_magic(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic
    chunks, extra_attrs, new_template = chunks_formatter.chunks(
                                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 55, in chunks
    chunks, extra_attrs = self._vformat_chunks(
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 228, in _vformat_chunks
    for literal_text, field_name, format_spec, conversion in self.parse(format_string):
                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/string.py", line 288, in parse
    return _string.formatter_parser(format_string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, got dict
Invalid type dict for attribute 'logfire.msg_template' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Invalid type dict for attribute 'logfire.msg' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Exception while exporting Span.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 113, in on_end
    self.span_exporter.export((span,))
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 92, in export
    self._log_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 371, in _log_span
    self._print_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 122, in _print_span
    self._console.print(Text.assemble(*parts))
                        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 397, in assemble
    append(*part)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 979, in append
    raise TypeError("Only str or Text can be appended to Text")
TypeError: Only str or Text can be appended to Text
{'name': "Kevin 'Brett\\u0000 Stakelin"}
----
17:02:35.653 JSON data_3: {'name': "Kevin 'Brett\x00 Stakelin"}

Caught an internal error in Logfire. Your code should still be running fine, just with less telemetry. This is just logging the internal error.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/test_pydantic.py", line 26, in <module>
    logfire.info(data_3)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 355, in info
    self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 679, in log
    msg, extra_attrs, msg_template = logfire_format_with_magic(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic
    chunks, extra_attrs, new_template = chunks_formatter.chunks(
                                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 55, in chunks
    chunks, extra_attrs = self._vformat_chunks(
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 228, in _vformat_chunks
    for literal_text, field_name, format_spec, conversion in self.parse(format_string):
                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/string.py", line 288, in parse
    return _string.formatter_parser(format_string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, got dict
Invalid type dict for attribute 'logfire.msg_template' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Invalid type dict for attribute 'logfire.msg' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Exception while exporting Span.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 113, in on_end
    self.span_exporter.export((span,))
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 92, in export
    self._log_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 371, in _log_span
    self._print_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 122, in _print_span
    self._console.print(Text.assemble(*parts))
                        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 397, in assemble
    append(*part)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 979, in append
    raise TypeError("Only str or Text can be appended to Text")
TypeError: Only str or Text can be appended to Text
{'name': "Kevin 'Brett\x00 Stakelin"}
----
Logfire project URL: https://logfire.pydantic.dev/vincentdavis/app-gotta-bike
17:02:36.144 Row: {'error': '', 'name': 'CX All SS 4-5', 'race_id': 1043765, 'ye..._modified': datetime.datetime(2024, 9, 4, 16, 34, 14, 372947)}
17:02:36.147 JSON data: [{'place': '1', 'points': '245.29', 'name': 'Chris Hamilton', ...': '51:22.6', 'usac_number': '378190', 'bib': '', 'team': ''}]
Exception while exporting Span batch.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 360, in _export_batch
    self.span_exporter.export(self.spans_list[:idx])  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/remove_pending.py", line 45, in export
    return super().export(result)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/wrapper.py", line 24, in export
    return self.wrapped_exporter.export(spans)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/otlp.py", line 194, in export
    return super().export(spans)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/wrapper.py", line 24, in export
    return self.wrapped_exporter.export(spans)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/otlp.py", line 244, in export
    return super().export(spans)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/wrapper.py", line 24, in export
    return self.wrapped_exporter.export(spans)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py", line 187, in export
    serialized_data = self._serialize_spans(spans)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py", line 150, in _serialize_spans
    return encode_spans(spans).SerializePartialToString()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py", line 56, in encode_spans
    resource_spans=_encode_resource_spans(sdk_spans)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py", line 79, in _encode_resource_spans
    pb2_span = _encode_span(sdk_span)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py", line 114, in _encode_span
    return PB2SPan(
           ^^^^^^^^
TypeError: bad argument type for built-in operation
Caught an internal error in Logfire. Your code should still be running fine, just with less telemetry. This is just logging the internal error.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/test_pydantic.py", line 51, in <module>
    logfire.info(row["results"])
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 355, in info
    self.log('info', msg_template, attributes, tags=_tags, exc_info=_exc_info)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/main.py", line 679, in log
    msg, extra_attrs, msg_template = logfire_format_with_magic(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic
    chunks, extra_attrs, new_template = chunks_formatter.chunks(
                                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 55, in chunks
    chunks, extra_attrs = self._vformat_chunks(
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 228, in _vformat_chunks
    for literal_text, field_name, format_spec, conversion in self.parse(format_string):
                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/string.py", line 288, in parse
    return _string.formatter_parser(format_string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, got list
Invalid type dict in attribute 'logfire.msg_template' value sequence. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or None
Invalid type dict in attribute 'logfire.msg' value sequence. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or None
Exception while exporting Span.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 113, in on_end
    self.span_exporter.export((span,))
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 92, in export
    self._log_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 371, in _log_span
    self._print_span(span)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 122, in _print_span
    self._console.print(Text.assemble(*parts))
                        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 397, in assemble
    append(*part)
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/rich/text.py", line 979, in append
    raise TypeError("Only str or Text can be appended to Text")
TypeError: Only str or Text can be appended to Text
Exception while exporting Span batch.
Traceback (most recent call last):
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 360, in _export_batch
    self.span_exporter.export(self.spans_list[:idx])  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/remove_pending.py", line 45, in export
    return super().export(result)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/wrapper.py", line 24, in export
    return self.wrapped_exporter.export(spans)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/otlp.py", line 194, in export
    return super().export(spans)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/wrapper.py", line 24, in export
    return self.wrapped_exporter.export(spans)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/otlp.py", line 244, in export
    return super().export(spans)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/logfire/_internal/exporters/wrapper.py", line 24, in export
    return self.wrapped_exporter.export(spans)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py", line 187, in export
    serialized_data = self._serialize_spans(spans)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py", line 150, in _serialize_spans
    return encode_spans(spans).SerializePartialToString()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py", line 56, in encode_spans
    resource_spans=_encode_resource_spans(sdk_spans)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py", line 79, in _encode_resource_spans
    pb2_span = _encode_span(sdk_span)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vincentdavis/DEV/REPO/_GOTTS.BIKE/app_gotta_bike/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py", line 114, in _encode_span
    return PB2SPan(
           ^^^^^^^^
TypeError: bad argument type for built-in operation

@alexmojaki
Copy link
Contributor

Those errors come from directly logging a dict object instead of a string, i.e. logfire.info(data_1).

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

No branches or pull requests

2 participants