Skip to content

DocumentUrl in message_history is missing "_media_type" #2394

@PhilippAlbrecht-KR

Description

@PhilippAlbrecht-KR

Initial Checks

Description

Hey Hey,
so this might be a bit special but I am trying to use a mcp client, that when given a pdf document I want it to do mcp stuff (create, update, delete entities on the server). In case information is lacking I want the client to be able to "chat" with the server(user who via frontend interacts with server) and so I am sending the history to the server and back to the client on every interaction. But it crashes with
AttributeError: 'DocumentUrl' object has no attribute '_media_type'. Did you mean: 'media_type'?
But this only happens when I use the history. I did some very crude hardcoded test to see what happens if I were to add the _media_type from the document. And this would work.
So I am wondering is message_history with a document in the loop bugged or am I simply doing it wrong?

Example Code

MCP agent is a MCPServerStreamableHTTPClient similar to the doc

CodeSnippet problematic part:
    try:
        async with mcp_agent:
            history = []
            if request.history:
                history = (
                    ModelMessagesTypeAdapter.validate_python(request.history) or []
                )

            if request.documentUrl:
                docUrl = DocumentUrl(url=request.documentUrl)

            agent_request = (
                [request.prompt, docUrl] if request.documentUrl else [request.prompt]
            )

            copyOfHistory = history.copy()
            if copyOfHistory:
                copyOfHistory[0].parts[0].content[1]._media_type = docUrl.media_type <<< this works (obviously only for the first time)

            result = await mcp_agent.run(
                agent_request,
                message_history=history,
            )

            raw_messages = result.all_messages()
            messages = to_jsonable_python(raw_messages)
            output = result.output
            return McpResponse(output=output, messages=messages)
    except Exception as e:
        logger.error(f"Error in MCP client: {str(e)}", exc_info=True)
        raise e

Python, Pydantic AI & LLM client version

Python 3.13.3
pydantic-ai-slim[mcp] 0.4.10
LLM gemini-flash-2.5 (via vertex ai)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions