Skip to content

Conversation

@siddhantbhagat8
Copy link

@siddhantbhagat8 siddhantbhagat8 commented Dec 2, 2025

Description

Changed the insertion logic to insert instructions after all system prompt parts

Simple append:

  1. anthropic.py
  2. google.py
  3. gemini.py
  4. bedrock.py

Insert at a particular position:
For models that build a messages list, count SystemPromptPart instances and insert at that index

  1. openai.py
  2. cohere.py
  3. groq.py
  4. mistral.py
  5. huggingface.py

Testing

I ran the tests locally, everything passed. If I should add a test for this change, please let me know the correct location for the test :)

Copy link
Collaborator

@DouweM DouweM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddhantbhagat8 Thanks Siddhant! Please look at my notes and add a test in test_agent.py, by using both system_prompt and instructions decorators on one agent

"""Just maps a `pydantic_ai.Message` to a `cohere.ChatMessageV2`."""
cohere_messages: list[ChatMessageV2] = []
system_prompt_count = sum(
1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in a wrong index when the system prompt parts are not all of the beginning, and it seems brittle to use an index derived from Pydantic AI messages in a list named cohere_messages, which may or may not map 1:1. So I think we should count system parts at the start of the actual cohere_messages list instead

"""Just maps a `pydantic_ai.Message` to a `groq.types.ChatCompletionMessageParam`."""
groq_messages: list[chat.ChatCompletionMessageParam] = []
system_prompt_count = sum(
1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as up

"""Just maps a `pydantic_ai.Message` to a `huggingface_hub.ChatCompletionInputMessage`."""
hf_messages: list[ChatCompletionInputMessage | ChatCompletionOutputMessage] = []
system_prompt_count = sum(
1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same :)

) -> list[MistralMessages]:
"""Just maps a `pydantic_ai.Message` to a `MistralMessage`."""
mistral_messages: list[MistralMessages] = []
system_prompt_count = sum(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same!

for message in messages:
for part in message.parts:
if isinstance(part, SystemPromptPart):
system_prompt_count += 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same :)

assert isinstance(instructions, str)
openai_messages.insert(0, responses.EasyInputMessageParam(role='system', content=instructions))
system_prompt_count = sum(
1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here as well. We should find the right index in the actual openai_messages list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants