Skip to content

fix: handle content=None and non-dict messages in format_content() - #1060

Merged
ishanjainn merged 1 commit into
openlit:mainfrom
saivedant169:fix/format-content-none-handling
Mar 18, 2026
Merged

ishanjainn merged 1 commit into
openlit:mainfrom
saivedant169:fix/format-content-none-handling

Conversation

@saivedant169

@saivedant169 saivedant169 commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Issue number: #1059

Change description:

format_content() serialized the entire messages list into a single user: span entry when content was None or messages were non-dict objects. This caused O(n²) prompt explosion in observability backends.

Root cause: message.get("content", "") or message.content returns None when the key exists with value None (.get() returns None, not ""), then None or message.content raises AttributeError on dicts. The bare except: catches this and falls back to str(messages) — the entire list.

Fix:

  • Check isinstance(message, dict) before using .get()
  • Handle content is None explicitly — normal for tool-call assistant turns, now summarized as [N tool call(s)]
  • Use getattr() for non-dict message objects (Pydantic models, LangChain messages)
  • Replace bare except: with except Exception:
  • Fallback stringifies the single failing message, not the entire messages list

Checklist

  • PR name follows conventional commit format: fix: ....
  • I have reviewed the contributing guidelines
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

Summary by Sourcery

Fix formatting of OpenAI messages when content is missing or messages are non-dict objects to prevent incorrect serialization and excessive prompts.

Bug Fixes:

  • Correct message role and content extraction in format_content() for dict and non-dict message types, including cases where content is None.
  • Avoid serializing the entire messages list when a single message fails to parse, limiting fallback to the offending message only.

format_content() was stringifying the entire messages list into a
single 'user:' span entry when content was None or messages were
non-dict objects. This caused O(n²) prompt explosion in observability
backends.

- Check isinstance(message, dict) before using .get()
- Handle content=None explicitly (normal for tool-call assistant turns)
- Use getattr() for non-dict message objects
- Replace bare except with except Exception
- Fallback stringifies single message, not entire list

Fixes openlit#1059
@saivedant169
saivedant169 requested review from a team and ishanjainn as code owners March 16, 2026 15:52
@sourcery-ai

sourcery-ai Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refines format_content() to safely handle dict and non-dict message objects with None or missing content, preventing accidental serialization of the entire messages list and reducing error scope.

File-Level Changes

Change Details Files
Harden message role/content extraction in format_content() for dicts and message objects, and narrow the error fallback behavior.
  • Branch on isinstance(message, dict) to separately handle dict-based messages and message objects
  • For dict messages, default role to 'user', treat content=None as a normal case, and summarize tool_calls as a compact '[N tool call(s)]' string when present
  • For non-dict messages, use getattr() to safely read role and content with sensible defaults and normalize None content to an empty string
  • Replace the bare except: with except Exception: and change the fallback to stringify only the failing message instead of the full messages list
  • Preserve existing handling for list-based content after content normalization
sdk/python/src/openlit/instrumentation/openai/utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ishanjainn
ishanjainn merged commit ade2e2f into openlit:main Mar 18, 2026
4 checks passed
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