microsoft / microsoft/agent-framework

Python: [Python] Large AgentResponse in entity set_result() causes .NET host to roll back entire entity state

Open
#3,884 0 comments 1 reaction 1 assignee View on GitHub

@larohra is already working on this.

Since Feb 13, 2026.

.NET hosting python
Dominant language
Python
Stars
13.6k
Forks
2.3k
Avg merge
2d 45m
Merged PRs (30d)
358

Description

## Bug Description

When a Durable Functions entity produces a large `AgentResponse` (e.g. from MCP tool calls returning verbose API payloads), `context.set_result(result.to_dict())` in `_entities.py` causes the .NET Durable Functions host to fail with:

```
System.ArgumentException: Out of proc orchestrators must return a valid JSON schema.
```

Because the .NET host treats `set_state()` and `set_result()` as a single atomic transaction, this failure **rolls back both** — including the response already written to entity state by `entity.run()` → `set_state()`. The HTTP polling endpoint then never finds the response, and the request times out.

## Root Cause

In [`_entities.py` line 86](https://github.com/microsoft/agent-framework/blob/main/python/packages/azurefunctions/agent_framework_azurefunctions/_entities.py#L86):

```python
result = await entity.run(request)
context.set_result(result.to_dict())
```

`result.to_dict()` serializes the **entire** `AgentResponse`, which for MCP tool calls can include deeply nested JSON objects, large text blobs, or complex structures from external APIs. The .NET Durable Functions host receives this over gRPC and fails to process it, rejecting the entire entity execution.

## Impact

- **"Say hello"** and other simple queries work fine (small response → `set_result()` succeeds → transaction commits).
- **MCP tool calls** or any agent interaction that produces a large response fails silently — the agent actually completes successfully (visible in traces), but the result is lost due to the state rollback.
- The HTTP polling endpoint returns a timeout, giving the user no indication that the agent actually succeeded.

## Steps to Reproduce

1. Deploy an `AgentFunctionApp` with an agent that uses `MCPStdioTool` (e.g. `mcp-atlassian` for Jira)
2. Send a request that triggers a tool call returning a large payload (e.g. "List all Jira projects")
3. Observe in Application Insights:
- Agent traces show successful execution with full MCP tool response
- Immediately followed by: `Function 'dafx- (Entity)' failed with an error. Reason: Internal error: System.ArgumentException: Out of proc orchestrators must return a valid JSON schema`
4. HTTP polling returns timeout — entity state was rolled back

## Proposed Fix

Pass only a compact summary to `set_result()`. The HTTP trigger path does **not** depend on `set_result()` — it reads from `read_entity_state()`, which uses `set_state()`. Only orchestrations using `call_entity()` consume `set_result()`.

```python
result = await entity.run(request)
# Full response is already persisted via set_state() inside entity.run().
# Pass a compact result to avoid .NET host serialization failures.
compact_result = {
"status": "ok",
"text": (result.text or "")[:4000],
}
context.set_result(compact_result)
```

**Trade-off:** Orchestrations calling the entity via `call_entity()` would receive only the compact result instead of the full `AgentResponse`. They could be updated to read the full response from entity state if needed.

## Environment

- **Package:** `agent-framework-azurefunctions` (PyPI)
- **Python:** 3.11
- **Platform:** Azure Functions Linux Consumption Plan
- **Durable Task Scheduler:** Consumption SKU
- **File:** `python/packages/azurefunctions/agent_framework_azurefunctions/_entities.py`, line 86

## Labels

`bug`, `python`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.