Repository navigation
fix: handle content=None and non-dict messages in format_content() - #1060
Merged
ishanjainn merged 1 commit intoMar 18, 2026
Merged
Conversation
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
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefines 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
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
ishanjainn
approved these changes
Mar 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue number: #1059
Change description:
format_content()serialized the entire messages list into a singleuser:span entry whencontentwasNoneor messages were non-dict objects. This caused O(n²) prompt explosion in observability backends.Root cause:
message.get("content", "") or message.contentreturnsNonewhen the key exists with valueNone(.get()returnsNone, not""), thenNone or message.contentraisesAttributeErroron dicts. The bareexcept:catches this and falls back tostr(messages)— the entire list.Fix:
isinstance(message, dict)before using.get()content is Noneexplicitly — normal for tool-call assistant turns, now summarized as[N tool call(s)]getattr()for non-dict message objects (Pydantic models, LangChain messages)except:withexcept Exception:message, not the entiremessageslistChecklist
fix: ....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: