Skip to main content

[Python] Why is Sentry reporting "broken repr" in my stack trace?

Issue

Sentry is reporting "broken repr" as a local variable in my stack trace:
​

Screenshot 2024-08-01 at 10.13.02 AM.png


Applies To

  • All SaaS Customers and Self-Hosted Users

  • Error Monitoring

  • Python SDK
    ​

Resolution

Every object in Python can have a __repr__() function that returns a string representation of that object. This function is called when serializing objects, before they are sent to Sentry.
​

The "broken repr" message appears in Sentry when it cannot obtain a string representation of an object due to an issue with the __repr__() function. Even if the syntax of the __repr__() function appears correct, there are a few common reasons it might still fail:

  1. Missing Attributes:

    The __repr__() method might fail if it tries to access an attribute that does not exist in the object. Ensure that all attributes used in __repr__() are present.
    ​

  2. Class Property Issues:

    The method might fail if the object does not have a __class__ property. Verify that the object is properly instantiated from a class.
    ​

  3. Context-Specific Failures:

    Sometimes, the failure can be context-specific. Testing repr(someobj) where someobj is the same as the self in the Sentry error can help identify if the issue is with the specific instance or the method itself.
    ​

By ensuring that all necessary attributes and properties are present and correctly handled, you can prevent the "broken repr" issue.

Did this answer your question?