Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
"type": "llm",
"children": [],
"input": [
{
"content": "Bonjour mon ami!",
"role": "user"
},
{
"content": [
{
Expand All @@ -77,6 +73,10 @@
}
],
"role": "system"
},
{
"content": "Bonjour mon ami!",
"role": "user"
}
],
"output": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ span_tree:
│ └── anthropic.messages.create [llm]
│ input: [
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ },
│ {
│ "content": [
│ {
│ "text": "translate to english",
Expand All @@ -68,6 +64,10 @@ span_tree:
│ }
│ ],
│ "role": "system"
│ },
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ }
│ ]
│ output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
"type": "llm",
"children": [],
"input": [
{
"content": "Bonjour mon ami!",
"role": "user"
},
{
"content": [
{
Expand All @@ -77,6 +73,10 @@
}
],
"role": "system"
},
{
"content": "Bonjour mon ami!",
"role": "user"
}
],
"output": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ span_tree:
│ └── anthropic.messages.create [llm]
│ input: [
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ },
│ {
│ "content": [
│ {
│ "text": "translate to english",
Expand All @@ -68,6 +64,10 @@ span_tree:
│ }
│ ],
│ "role": "system"
│ },
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ }
│ ]
│ output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
"type": "llm",
"children": [],
"input": [
{
"content": "Bonjour mon ami!",
"role": "user"
},
{
"content": [
{
Expand All @@ -77,6 +73,10 @@
}
],
"role": "system"
},
{
"content": "Bonjour mon ami!",
"role": "user"
}
],
"output": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ span_tree:
│ └── anthropic.messages.create [llm]
│ input: [
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ },
│ {
│ "content": [
│ {
│ "text": "translate to english",
Expand All @@ -68,6 +64,10 @@ span_tree:
│ }
│ ],
│ "role": "system"
│ },
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ }
│ ]
│ output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
"type": "llm",
"children": [],
"input": [
{
"content": "Bonjour mon ami!",
"role": "user"
},
{
"content": [
{
Expand All @@ -77,6 +73,10 @@
}
],
"role": "system"
},
{
"content": "Bonjour mon ami!",
"role": "user"
}
],
"output": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ span_tree:
│ └── anthropic.messages.create [llm]
│ input: [
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ },
│ {
│ "content": [
│ {
│ "text": "translate to english",
Expand All @@ -68,6 +64,10 @@ span_tree:
│ }
│ ],
│ "role": "system"
│ },
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ }
│ ]
│ output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
"type": "llm",
"children": [],
"input": [
{
"content": "Bonjour mon ami!",
"role": "user"
},
{
"content": [
{
Expand All @@ -77,6 +73,10 @@
}
],
"role": "system"
},
{
"content": "Bonjour mon ami!",
"role": "user"
}
],
"output": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ span_tree:
│ └── anthropic.messages.create [llm]
│ input: [
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ },
│ {
│ "content": [
│ {
│ "text": "translate to english",
Expand All @@ -68,6 +64,10 @@ span_tree:
│ }
│ ],
│ "role": "system"
│ },
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ }
│ ]
│ output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
"type": "llm",
"children": [],
"input": [
{
"content": "Bonjour mon ami!",
"role": "user"
},
{
"content": [
{
Expand All @@ -77,6 +73,10 @@
}
],
"role": "system"
},
{
"content": "Bonjour mon ami!",
"role": "user"
}
],
"output": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ span_tree:
│ └── anthropic.messages.create [llm]
│ input: [
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ },
│ {
│ "content": [
│ {
│ "text": "translate to english",
Expand All @@ -68,6 +64,10 @@ span_tree:
│ }
│ ],
│ "role": "system"
│ },
│ {
│ "content": "Bonjour mon ami!",
│ "role": "user"
│ }
│ ]
│ output: {
Expand Down
47 changes: 47 additions & 0 deletions js/src/instrumentation/plugins/anthropic-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
parseMetricsFromUsage,
aggregateAnthropicStreamChunks,
processAttachmentsInInput,
coalesceInput,
} from "./anthropic-plugin";
import type { StartEvent } from "../core";
import { Attachment } from "../../logger";
Expand Down Expand Up @@ -918,3 +919,49 @@ describe("processAttachmentsInInput", () => {
expect(result[0].type).toBe("image");
});
});

describe("coalesceInput", () => {
it("should place the system message before the conversation messages", () => {
const messages = [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there" },
{ role: "user", content: "How are you?" },
];

const result = coalesceInput(messages, "You are a helpful assistant.");

expect(result).toEqual([
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there" },
{ role: "user", content: "How are you?" },
]);
});

it("should support system content as an array of text blocks", () => {
const messages = [{ role: "user", content: "Bonjour" }];
const system = [
{ type: "text" as const, text: "translate to english" },
{ type: "text" as const, text: "only the answer no other text" },
];

const result = coalesceInput(messages, system);

expect(result[0]).toEqual({ role: "system", content: system });
expect(result[1]).toEqual({ role: "user", content: "Bonjour" });
});

it("should return messages unchanged when there is no system prompt", () => {
const messages = [{ role: "user", content: "Hello" }];

expect(coalesceInput(messages, undefined)).toEqual(messages);
});

it("should not mutate the original messages array", () => {
const messages = [{ role: "user", content: "Hello" }];

coalesceInput(messages, "system prompt");

expect(messages).toEqual([{ role: "user", content: "Hello" }]);
});
});
8 changes: 5 additions & 3 deletions js/src/instrumentation/plugins/anthropic-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,16 +1051,18 @@ export function processAttachmentsInInput(input: unknown): unknown {

/**
* Convert Anthropic args to the single "input" field Braintrust expects.
* Combines messages array with system message if present.
* Combines messages array with system message if present. The system message
* is placed first to match the order the model sees and the chat-message
* convention used elsewhere (e.g. the playground).
*/
function coalesceInput(
export function coalesceInput(
messages: AnthropicInputMessage[],
system: AnthropicCreateParams["system"],
): AnthropicInputMessage[] {
// Make a copy because we're going to mutate it
const input = (messages || []).slice();
if (system) {
input.push({ role: "system", content: system });
input.unshift({ role: "system", content: system });
}
return input;
}