Skip to content

evals - RuntimeError: Attempted to exit cancel scope in a different task than it was entered inΒ #2818

@g-eoj

Description

@g-eoj

Initial Checks

Description

When setting max_concurrency to anything other than one, eventually the RuntimeError: Attempted to exit cancel scope in a different task than it was entered in error will pop up for evals with ~10 or more cases. It appears to be connected to using toolsets created from MCP servers.

I followed https://ai.pydantic.dev/api/mcp/#pydantic_ai.mcp.MCPServerStreamableHTTP - tried using this pattern everywhere an agent is used:

import some_agent

...

async with some_agent:
    run = await some_agent.run(...)

...

Example Code

Here is a simple reproduction - this should work right?

import asyncio

from pydantic_ai.mcp import MCPServerStdio
from pydantic_evals import Case, Dataset
from pydantic_evals.evaluators import Evaluator, EvaluatorContext


class MyEvaluator(Evaluator[str, bool]):
    def evaluate(self, ctx: EvaluatorContext[str, bool]) -> bool:
        return True


mcp_server = MCPServerStdio('python', ['-m', 'tests.mcp_server'])


async def run(case: str) -> bool:
    async with mcp_server:
        await mcp_server.list_tools()
    return True


async def main():
    cases: list[Case[str, bool, dict[str, str]]] = []
    for _ in range(10):
        case = Case(
            name=str(_),
            inputs='',
            expected_output=True,
            metadata={'difficulty': 'easy'},
        )
        cases.append(case)

    dataset = Dataset(
        cases=cases,
        evaluators=[MyEvaluator()],
    )

    report = await dataset.evaluate(run, max_concurrency=4)
    report.print()


if __name__ == '__main__':
    asyncio.run(main())

Python, Pydantic AI & LLM client version

Pydantic AI 1.0.1
Python 3.12.7

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions