Fixes invalid_request_error for tool calling with Anthropic models#192
Merged
zachdaniel merged 1 commit intoash-project:mainfrom Apr 24, 2026
Conversation
Anthropic does not allow "prefilling assitant messages", meaning the last message of the `messages` parameter to ReqLLM `generate_object/4` may not have the role `:assistant`. See: ash-project#191 This commit prevents run_loop/10 and stream_iteration/1 in AshAi.ToolLoop from appending assistant messages to the context when the model provider is anthropic. --- I've changed `defp resolve_model(model, _opts), do: model` to return `ReqLLM.model!(model)` in `AshAi.ToolLoop`. This makes working with the model inside the module simpler ans seems to be recommended by ReqLLM https://github.com/agentjido/req_llm/blob/main/lib/req_llm.ex#L353 Would have done the same in `AshAi.Actions.Prompt` for resolve_model_spec since model is passed from Prompt to ToolLoop and people using the fn variant of resolve_model this would be a breaking change.
zachdaniel
approved these changes
Apr 24, 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.
Anthropic does not allow "prefilling assitant messages", meaning the last message of the
messagesparameter to ReqLLMgenerate_object/4may not have the role:assistant.See: #191
This commit prevents run_loop/10 and stream_iteration/1 in AshAi.ToolLoop from appending assistant messages to the context when the model provider is anthropic.
I've changed
defp resolve_model(model, _opts), do: modelto returnReqLLM.model!(model)inAshAi.ToolLoop. This makes working with the model inside the module simpler ans seems to be recommended by ReqLLM https://github.com/agentjido/req_llm/blob/main/lib/req_llm.ex#L353Would have done the same in
AshAi.Actions.Promptfor resolve_model_spec since model is passed from Prompt to ToolLoop and people using the fn variant of resolve_model this would be a breaking change.Contributor checklist
Leave anything that you believe does not apply unchecked.