Skip to content

[Bug]: APIKeySecurityScheme implementation parsing falied in the client #220

@FailedNamed

Description

@FailedNamed

What happened?

Hello everyone,

I am trying to do an example of an A2A client-server communication with APIKeySecurityScheme authentication.

This problem was mentioned at #165. That issue was closed, but now the problem occurs again.

Environment

  • Windows 10
  • Python 3.13
  • pydantic==2.11.7
  • a2a-sdk==0.2.8

Agent Card Scheme

api_key_scheme = {
    'type': 'apiKey',
    'name': 'X-API-KEY',
    'in': 'header'
}

agent_card = AgentCard(
    name='Currency Agent',
    description='Helps with exchange rates for currencies',
    url=f'http://{host}:{port}/',
    version='1.0.0',
    defaultInputModes=CurrencyAgent.SUPPORTED_CONTENT_TYPES,
    defaultOutputModes=CurrencyAgent.SUPPORTED_CONTENT_TYPES,
    capabilities=capabilities,
    skills=[skill],
    securitySchemes={
        "api_key": APIKeySecurityScheme.model_validate(api_key_scheme)
    },
    security=[
        {
            'api_key': []
        }
    ],
)

The Issue

The APIKeySecurityScheme's in field is using an alias. But when the SDK server does model_dump for the AgentCard, it doesn't open by_alias, which causes the .well-known/agent.json endpoint to always return a JSON with "in_" instead of "in":

Example returned content:

{
  "capabilities": {"pushNotifications": true, "streaming": true},
  "defaultInputModes": ["text", "text/plain"],
  "defaultOutputModes": ["text", "text/plain"],
  "description": "Helps with exchange rates for currencies",
  "name": "Currency Agent",
  "security": [{"api_key": []}],
  "securitySchemes": {
    "api_key": {
      "in_": "header",
      "name": "X-API-KEY",
      "type": "apiKey"
    }
  },
  "skills": [{
    "description": "Helps with exchange values between various currencies",
    "examples": ["What is exchange rate between USD and GBP?"],
    "id": "convert_currency",
    "name": "Currency Exchange Rates Tool",
    "tags": ["currency conversion", "currency exchange"]
  }],
  "url": "http://xxxx:10000/",
  "version": "1.0.0"
}

Relevant Code

class APIKeySecurityScheme(BaseModel):
    """API Key security scheme."""

    description: str | None = None
    in_: In = Field(..., alias='in')
    name: str
    type: Literal['apiKey'] = 'apiKey'

async def _handle_get_agent_card(self, request: Request) -> JSONResponse:
    """Handles GET requests for the agent card endpoint."""
    # The public agent card is a direct serialization of the agent_card
    # provided at initialization.
    return JSONResponse(
        self.agent_card.model_dump(mode='json', exclude_none=True)
    )

Impact

This makes the sdk client A2ACardResolver.get_agent_card parse the card response unsuccessfully:

agent_card = AgentCard.model_validate(agent_card_data)

Relevant Log Output

pydantic_core._pydantic_core.ValidationError: 7 validation errors for AgentCard
securitySchemes.api_key.APIKeySecurityScheme.in
  Field required [type=missing, input_value={'in_': 'header', 'name':...-KEY', 'type': 'apiKey'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing
securitySchemes.api_key.HTTPAuthSecurityScheme.scheme
  Field required [type=missing, input_value={'in_': 'header', 'name':...-KEY', 'type': 'apiKey'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing
securitySchemes.api_key.HTTPAuthSecurityScheme.type
  Input should be 'http' [type=literal_error, input_value='apiKey', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error
securitySchemes.api_key.OAuth2SecurityScheme.flows

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions