-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): Strip inline media from multimodal content before stringification #19540
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
Open
RulaKhaled
wants to merge
12
commits into
develop
Choose a base branch
from
fix-langchain-scrubbing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+287
−6
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fac656c
fix(core): Improve message truncation for multimodal content and norm…
RulaKhaled fae419d
take out media stripping to its own file
RulaKhaled 25c9a93
we should fit truncation in 20KB
RulaKhaled 7511263
fix test
RulaKhaled 598e46a
quick refactor
RulaKhaled 9934f5b
fix(core): Strip inline media from multimodal content before stringif…
RulaKhaled 7cf24ab
refacotr
RulaKhaled 7c6deae
linter
RulaKhaled 0e78b19
Merge branch 'develop' into fix-langchain-scrubbing
RulaKhaled efae4e8
fix format
RulaKhaled 4d5760f
try/catch the stringified json
RulaKhaled b670bcd
try catch
RulaKhaled File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { GEN_AI_INPUT_MESSAGES_ATTRIBUTE } from '../../../src/tracing/ai/gen-ai-attributes'; | ||
| import type { LangChainMessage } from '../../../src/tracing/langchain/types'; | ||
| import { extractChatModelRequestAttributes, normalizeLangChainMessages } from '../../../src/tracing/langchain/utils'; | ||
|
|
||
| describe('normalizeLangChainMessages', () => { | ||
| it('normalizes messages with _getType()', () => { | ||
| const messages = [ | ||
| { | ||
| _getType: () => 'human', | ||
| content: 'Hello', | ||
| }, | ||
| { | ||
| _getType: () => 'ai', | ||
| content: 'Hi there!', | ||
| }, | ||
| ] as unknown as LangChainMessage[]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| expect(result).toEqual([ | ||
| { role: 'user', content: 'Hello' }, | ||
| { role: 'assistant', content: 'Hi there!' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('normalizes messages with type property', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { type: 'human', content: 'Hello' }, | ||
| { type: 'ai', content: 'Hi!' }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| expect(result).toEqual([ | ||
| { role: 'user', content: 'Hello' }, | ||
| { role: 'assistant', content: 'Hi!' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('normalizes messages with role property', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { role: 'user', content: 'Hello' }, | ||
| { role: 'assistant', content: 'Hi!' }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| expect(result).toEqual([ | ||
| { role: 'user', content: 'Hello' }, | ||
| { role: 'assistant', content: 'Hi!' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('normalizes serialized LangChain format', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { | ||
| lc: 1, | ||
| id: ['langchain_core', 'messages', 'HumanMessage'], | ||
| kwargs: { content: 'Hello from serialized' }, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| expect(result).toEqual([{ role: 'user', content: 'Hello from serialized' }]); | ||
| }); | ||
|
|
||
| describe('multimodal content media stripping', () => { | ||
| const b64Data = `iVBORw0KGgoAAAANSUhEUgAAAAUA${'A'.repeat(200)}`; | ||
| const BLOB_SUBSTITUTE = '[Blob substitute]'; | ||
|
|
||
| it('strips base64 image_url from multimodal array content via _getType()', () => { | ||
| const messages = [ | ||
| { | ||
| _getType: () => 'human', | ||
| content: [ | ||
| { type: 'text', text: 'What color is in this image?' }, | ||
| { type: 'image_url', image_url: { url: `data:image/png;base64,${b64Data}` } }, | ||
| ], | ||
| }, | ||
| ] as unknown as LangChainMessage[]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0]!.role).toBe('user'); | ||
|
|
||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed).toHaveLength(2); | ||
| expect(parsed[0]).toEqual({ type: 'text', text: 'What color is in this image?' }); | ||
| expect(parsed[1].image_url.url).toBe(BLOB_SUBSTITUTE); | ||
| expect(result[0]!.content).not.toContain(b64Data); | ||
| }); | ||
|
|
||
| it('strips base64 data from Anthropic-style source blocks', () => { | ||
| const messages = [ | ||
| { | ||
| _getType: () => 'human', | ||
| content: [ | ||
| { type: 'text', text: 'Describe this image' }, | ||
| { | ||
| type: 'image', | ||
| source: { | ||
| type: 'base64', | ||
| media_type: 'image/png', | ||
| data: b64Data, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ] as unknown as LangChainMessage[]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed[1].source.data).toBe(BLOB_SUBSTITUTE); | ||
| expect(result[0]!.content).not.toContain(b64Data); | ||
| }); | ||
|
|
||
| it('strips base64 from inline_data (Google GenAI style)', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { | ||
| type: 'human', | ||
| content: [ | ||
| { type: 'text', text: 'Describe' }, | ||
| { inlineData: { mimeType: 'image/png', data: b64Data } }, | ||
| ] as unknown as string, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed[1].inlineData.data).toBe(BLOB_SUBSTITUTE); | ||
| expect(result[0]!.content).not.toContain(b64Data); | ||
| }); | ||
|
|
||
| it('strips base64 from input_audio content parts', () => { | ||
| const messages = [ | ||
| { | ||
| _getType: () => 'human', | ||
| content: [ | ||
| { type: 'text', text: 'What do you hear?' }, | ||
| { type: 'input_audio', input_audio: { data: b64Data } }, | ||
| ], | ||
| }, | ||
| ] as unknown as LangChainMessage[]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed[1].input_audio.data).toBe(BLOB_SUBSTITUTE); | ||
| expect(result[0]!.content).not.toContain(b64Data); | ||
| }); | ||
|
|
||
| it('preserves text-only array content without modification', () => { | ||
| const messages = [ | ||
| { | ||
| _getType: () => 'human', | ||
| content: [ | ||
| { type: 'text', text: 'First part' }, | ||
| { type: 'text', text: 'Second part' }, | ||
| ], | ||
| }, | ||
| ] as unknown as LangChainMessage[]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed).toEqual([ | ||
| { type: 'text', text: 'First part' }, | ||
| { type: 'text', text: 'Second part' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('strips media from serialized LangChain format with array content', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { | ||
| lc: 1, | ||
| id: ['langchain_core', 'messages', 'HumanMessage'], | ||
| kwargs: { | ||
| content: [ | ||
| { type: 'text', text: 'Describe this' }, | ||
| { type: 'image_url', image_url: { url: `data:image/png;base64,${b64Data}` } }, | ||
| ] as unknown as string, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed[1].image_url.url).toBe(BLOB_SUBSTITUTE); | ||
| expect(result[0]!.content).not.toContain(b64Data); | ||
| }); | ||
|
|
||
| it('strips media from messages with role property and array content', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { type: 'text', text: 'Look at this' }, | ||
| { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${b64Data}` } }, | ||
| ] as unknown as string, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed[1].image_url.url).toBe(BLOB_SUBSTITUTE); | ||
| expect(result[0]!.content).not.toContain(b64Data); | ||
| }); | ||
|
|
||
| it('strips media from messages with type property and array content', () => { | ||
| const messages: LangChainMessage[] = [ | ||
| { | ||
| type: 'human', | ||
| content: [ | ||
| { type: 'text', text: 'Check this' }, | ||
| { type: 'image_url', image_url: { url: `data:image/png;base64,${b64Data}` } }, | ||
| ] as unknown as string, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = normalizeLangChainMessages(messages); | ||
| const parsed = JSON.parse(result[0]!.content); | ||
| expect(parsed[1].image_url.url).toBe(BLOB_SUBSTITUTE); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('extractChatModelRequestAttributes with multimodal content', () => { | ||
| const b64Data = `iVBORw0KGgoAAAANSUhEUgAAAAUA${'A'.repeat(200)}`; | ||
|
|
||
| it('strips base64 from input messages attribute', () => { | ||
| const serialized = { id: ['langchain', 'chat_models', 'openai'], name: 'ChatOpenAI' }; | ||
| const messages: LangChainMessage[][] = [ | ||
| [ | ||
| { | ||
| _getType: () => 'human', | ||
| content: [ | ||
| { type: 'text', text: 'What is in this image?' }, | ||
| { type: 'image_url', image_url: { url: `data:image/png;base64,${b64Data}` } }, | ||
| ], | ||
| } as unknown as LangChainMessage, | ||
| ], | ||
| ]; | ||
|
|
||
| const attrs = extractChatModelRequestAttributes(serialized, messages, true); | ||
| const inputMessages = attrs[GEN_AI_INPUT_MESSAGES_ATTRIBUTE] as string | undefined; | ||
|
|
||
| expect(inputMessages).toBeDefined(); | ||
| expect(inputMessages).not.toContain(b64Data); | ||
| expect(inputMessages).toContain('[Blob substitute]'); | ||
| expect(inputMessages).toContain('What is in this image?'); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.