Skip to content

fix(ai): prevent duplicated system prompts in PydanticAI completions#8356

Merged
Light2Dark merged 2 commits intomarimo-team:mainfrom
fpreiss:main
Feb 18, 2026
Merged

fix(ai): prevent duplicated system prompts in PydanticAI completions#8356
Light2Dark merged 2 commits intomarimo-team:mainfrom
fpreiss:main

Conversation

@fpreiss
Copy link
Contributor

@fpreiss fpreiss commented Feb 18, 2026

I intercepted the outgoing API request and observed the following duplicated content in the system message during tab completion:

{
    "messages": [
        {
            "content": "You are a python code completion assistant... \nYou are a python code completion assistant...",
            "role": "system"
        },
        ...
    ]
}

Cause

This is caused by the internal concatenation logic of pydantic_ai.agent.__init__.Agent._get_instructions and how completion in marimo/_server/ai/providers.py is providing the system_prompt to both - the Agent's constructor and the agent.run method.

Fix

The fix removes instructions=system_prompt from agent.run(), leaving only the constructor-level instructions.

Notes

A similar duplication issue likely exists in stream_text() (also in providers.py), where system_prompt may be passed to both create_agent() and agent.run_stream(). I have not verified this e2e though.

📋 Checklist

  • I have read the contributor guidelines.
  • Tests have been added for the changes made.
  • Pull request title is a good summary of the changes - it will be used in the release notes.

## Summary

Add a test that verifies the `completion` method does not pass
redundant instructions to the PydanticAI agent, which causes
system prompt duplication in API requests.

## Description of Changes

This test currently fails because `system_prompt` is passed to both
the `Agent()` constructor (via `create_agent`) and `agent.run()`,
causing PydanticAI's `_get_instructions` to concatenate them.
## Summary

The `system_prompt` was being passed to both the `Agent` constructor and
`agent.run()`, causing PydanticAI's `_get_instructions` to duplicate it.
Remove the redundant `instructions` argument from `agent.run()`.

## Description of Changes

In `PydanticProvider.completion()`, `system_prompt` was already passed to
`create_agent()`, which initializes the agent with those instructions.
Passing it again to `agent.run()` caused duplication because
`Agent._get_instructions()` concatenates constructor-level instructions
with run-level `additional_instructions`.

Removing `instructions=system_prompt` from `agent.run()` resolves this.
@vercel
Copy link

vercel bot commented Feb 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Feb 18, 2026 8:57am

Request Review

@github-actions
Copy link

github-actions bot commented Feb 18, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@fpreiss
Copy link
Contributor Author

fpreiss commented Feb 18, 2026

recheck

@fpreiss
Copy link
Contributor Author

fpreiss commented Feb 18, 2026

I have read the CLA Document and I hereby sign the CLA

@Light2Dark Light2Dark added the bug Something isn't working label Feb 18, 2026
@Light2Dark
Copy link
Collaborator

Light2Dark commented Feb 18, 2026

Thanks @fpreiss , I think you are right. Do you also want to address the other parts? I believe they aren't hit because only on certain codepaths but would be good to get them

@fpreiss
Copy link
Contributor Author

fpreiss commented Feb 18, 2026

Yes, I'm willing to take care of this within the next week.

@Light2Dark Light2Dark merged commit 844436a into marimo-team:main Feb 18, 2026
37 of 43 checks passed
LiquidGunay pushed a commit to LiquidGunay/marimo that referenced this pull request Feb 21, 2026
…arimo-team#8356)

I intercepted the outgoing API request and observed the following
duplicated content in the system message during tab completion:

```json
{
    "messages": [
        {
            "content": "You are a python code completion assistant... \nYou are a python code completion assistant...",
            "role": "system"
        },
        ...
    ]
}
```

## Cause

This is caused by the internal concatenation logic of
`pydantic_ai.agent.__init__.Agent._get_instructions` and how
`completion` in `marimo/_server/ai/providers.py` is providing the
`system_prompt` to both - the `Agent`'s constructor and the `agent.run`
method.

## Fix

The fix removes `instructions=system_prompt` from `agent.run()`, leaving
only the constructor-level instructions.

## Notes

A similar duplication issue likely exists in `stream_text()` (also in
`providers.py`), where `system_prompt` may be passed to both
`create_agent()` and `agent.run_stream()`. I have not verified this e2e
though.

## 📋 Checklist

- [x] I have read the [contributor
guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md).
- [x] Tests have been added for the changes made.
- [x] Pull request title is a good summary of the changes - it will be
used in the [release
notes](https://github.com/marimo-team/marimo/releases).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants