Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions python/packages/foundry/agent_framework_foundry/_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,17 @@ async def _prepare_options(
)

# Prepare messages: extract system/developer messages as instructions
prepared_messages, _instructions = self._prepare_messages_for_azure_ai(messages)
prepared_messages, context_instructions = self._prepare_messages_for_azure_ai(messages)
prepare_options = dict(options)
base_instructions = cast(str | None, prepare_options.pop("instructions", None))
instructions = "\n".join(
instruction for instruction in (base_instructions, context_instructions) if instruction
)

# Call parent prepare_options (OpenAI Responses API format)
run_options = await super()._prepare_options(prepared_messages, options, **kwargs)
run_options = await super()._prepare_options(prepared_messages, prepare_options, **kwargs)
if instructions:
run_options["instructions"] = instructions

# Apply Azure AI schema transforms
if "input" in run_options and isinstance(run_options["input"], list):
Expand Down
29 changes: 29 additions & 0 deletions python/packages/foundry/tests/foundry/test_foundry_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ChatMiddleware,
ChatResponse,
ChatResponseUpdate,
Content,
Message,
tool,
)
Expand Down Expand Up @@ -202,6 +203,34 @@ def my_func() -> str:
}


async def test_raw_foundry_agent_chat_client_prepare_options_forwards_context_instructions() -> None:
mock_project = MagicMock()
mock_project.get_openai_client.return_value = MagicMock()

client = RawFoundryAgentChatClient(
project_client=mock_project,
agent_name="test-agent",
)

result = await client._prepare_options(
messages=[
Message(role="system", contents=[Content.from_text("Use available skills.")]),
Message(role="developer", contents=[Content.from_text("Prefer deterministic answers.")]),
Message(role="user", contents=[Content.from_text("run cats-name")]),
],
options={"instructions": "Base instructions."},
)

assert result["instructions"] == "Base instructions.\nUse available skills.\nPrefer deterministic answers."
assert result["input"] == [
{
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "run cats-name"}],
}
]


async def test_raw_foundry_agent_chat_client_prepare_options_strips_client_side_fields() -> None:
"""Test that _prepare_options strips model and tool-loop fields from run_options."""

Expand Down