Skip to content

#1262 - fix(genai): correct Anthropic system prompt placement and add provider unit tests#1588

Merged
apidash-dev merged 1 commit into
foss42:mainfrom
soumyarajbag:fix/anthropic-system-prompt-and-genai-provider-tests
May 17, 2026
Merged

#1262 - fix(genai): correct Anthropic system prompt placement and add provider unit tests#1588
apidash-dev merged 1 commit into
foss42:mainfrom
soumyarajbag:fix/anthropic-system-prompt-and-genai-provider-tests

Conversation

@soumyarajbag
Copy link
Copy Markdown
Contributor

PR Description

The Anthropic Messages API does not support role: "system" inside the messages array — sending it that way results in an invalid request.
This PR fixes AnthropicModel.createRequest to place the system prompt as a top-level "system" field, and moves only the user turn into
messages, matching the official Anthropic API spec.

Additionally adds a "Generate" fallback for empty userPrompt, consistent with the existing OpenAI provider behaviour.

Before:

"messages": [
  {"role": "system", "content": systemPrompt},  // invalid for Anthropic
  {"role": "user", "content": userPrompt},
]

After:
if (systemPrompt.isNotEmpty) "system": systemPrompt,  // top-level field
"messages": [
  {"role": "user", "content": userPrompt.isNotEmpty ? userPrompt : "Generate"},
]

Also adds missing unit tests for OpenAIModel, AnthropicModel, and OllamaModel — previously only GeminiModel had provider-level test coverage.

Related Issues

- Closes #1262 

Checklist

- I have gone through the contributing guide
- I have updated my branch and synced it with project main branch before making this PR
- I am using the latest Flutter stable branch (run flutter upgrade and verify)
- I have run the tests (flutter test) and all tests are passing

Added/updated tests?

- Yes
  - test/interface/model_providers/anthropic_test.dart — verifies system prompt is top-level, not in messages; streaming flag; empty prompt
fallbacks; output formatters
  - test/interface/model_providers/openai_test.dart — messages structure, system role, streaming, formatters
  - test/interface/model_providers/ollama_test.dart — OpenAI-compatible format, default config subset, formatters

OS on which you have developed and tested the feature?

- Windows
- macOS
- Linux

@apidash-dev
Copy link
Copy Markdown
Collaborator

Nice catch. Thanks for the fix.

@apidash-dev apidash-dev merged commit 3ddbbd6 into foss42:main May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants