Skip to content

Commit

Permalink
fix(models/anthropic): ensure that the last message isn't an assistan…
Browse files Browse the repository at this point in the history
…t one (#47)

* fix(models/anthropic): ensure that the last message isn't an assistant one

* Create sixty-zoos-greet.md
  • Loading branch information
charlypoly authored Jan 16, 2025
1 parent 7e1df83 commit ec689b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-zoos-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inngest/agent-kit": minor
---

fix(models/anthropic): ensure that the last message isn't an assistant one
7 changes: 7 additions & 0 deletions src/adapters/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export const requestParser: AgenticModel.RequestParser<Anthropic.AiModel> = (
[] as AiAdapter.Input<Anthropic.AiModel>["messages"]
);

// We need to patch the last message if it's an assistant message. This is a known limitation of Anthropic's API.
// cf: https://github.com/langchain-ai/langgraph/discussions/952#discussioncomment-10012320
const lastMessage = anthropicMessages[anthropicMessages.length - 1];
if (lastMessage?.role === "assistant") {
lastMessage.role = "user";
}

const request: AiAdapter.Input<Anthropic.AiModel> = {
system,
model: model.options.model,
Expand Down

0 comments on commit ec689b8

Please sign in to comment.