tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Agentic AI > ADK Agent Testing > Contract Testing ADK Agent Outputs with Pydantic

Contract Testing ADK Agent Outputs with Pydantic

Author: Venkata Sudhakar

ShopMax India's ADK agents are consumed by multiple downstream systems - a mobile app, a call center dashboard, and an email bot - each expecting responses in a specific format. Contract testing with Pydantic validates that agent outputs always conform to a defined schema, catching format regressions before they break consumers. Unlike prose assertions, Pydantic contracts are self-documenting and automatically generate clear error messages when a field is missing or has the wrong type.

A Pydantic contract defines the expected shape of the agent response: required fields, their types, and validation rules. In ADK, agents return structured output or text that gets parsed into a model. Contract tests parse the agent output and attempt to construct the Pydantic model - if model construction fails, the contract is broken. This runs in milliseconds and requires no LLM calls in the test itself, making it ideal for high-frequency CI runs.

The example shows ShopMax India defining Pydantic contracts for their order status agent and running contract validation in pytest. Both valid and invalid agent outputs are tested to ensure the contract accepts correct responses and rejects malformed ones.


It gives the following output,

test_valid_response_passes_contract PASSED
test_invalid_responses_fail_contract[bad_response0] PASSED
test_invalid_responses_fail_contract[bad_response1] PASSED
test_invalid_responses_fail_contract[bad_response2] PASSED

Version your Pydantic contracts alongside your agent code so breaking changes are explicit. When adding a new required field, first add it as Optional with a default, deploy the agent that starts returning it, then make it required in the next release. Use model.model_json_schema() to auto-generate API documentation for downstream consumers. Run contract tests in CI before integration tests so structural failures fail fast. For agents that return free-text responses, extract structured data with a regex or lightweight parser before validating against the contract.


 
  


  
bl  br