OpenResponses Types
The `openresponses-types` library provides a Python SDK for the OpenResponses specification, offering standardized data types and models for structured interactions, particularly for AI agents. It leverages Pydantic for robust data validation, serialization, and deserialization of response and prompt structures. The current version is 2.3.0.post1, with releases aligning with updates to the underlying OpenResponses specification.
Common errors
-
ModuleNotFoundError: No module named 'openresponses_types'
cause Attempting to import directly from the PyPI package name (openresponses-types) instead of the actual installed Python package (openresponses).fixChange your import statements from `from openresponses_types import ...` to `from openresponses.types import ...`. -
AttributeError: 'AgentResponse' object has no attribute 'json'
cause This error typically occurs when using Pydantic V1 methods (`.json()`, `.dict()`) on Pydantic V2 models. `openresponses-types` uses Pydantic V2.fixReplace `.json()` with `.model_dump_json()` and `.dict()` with `.model_dump()` to align with Pydantic V2 API changes. -
pydantic.ValidationError: X validation error for Y
cause The data provided to construct or validate an OpenResponse model does not conform to the schema (e.g., missing required fields, incorrect types, invalid enum values).fixCarefully review the traceback to identify the specific field causing the error. Check the OpenResponses specification or the model definitions (`.model_json_schema()`) to ensure your data matches the expected structure and types.
Warnings
- gotcha The PyPI package name (`openresponses-types`) is different from the top-level Python package name you import (`openresponses`). Always use `from openresponses.types import ...`.
- breaking This library explicitly requires Pydantic V2. Projects using Pydantic V1 will encounter conflicts and `pydantic.ValidationError` or `AttributeError` if Pydantic V1 methods are used.
- gotcha The `OpenResponse` specification is strict. Any data provided that does not conform to the defined schemas will result in `pydantic.ValidationError`.
Install
-
pip install openresponses-types
Imports
- OpenResponse
from openresponses_types import OpenResponse
from openresponses.types import OpenResponse
- AgentResponse
from openresponses.types import AgentResponse
- HumanResponse
from openresponses.types import HumanResponse
- ChatMLMessage
from openresponses.types import ChatMLMessage
from openresponses.types.model import ChatMLMessage
- Action
from openresponses.types.action import Action
Quickstart
from openresponses.types import AgentResponse, OpenResponse
from openresponses.types.model import ChatMLMessage
# Create an AgentResponse
agent_msg = ChatMLMessage(role="assistant", content="Hello, how can I help you?")
agent_response = AgentResponse(
model_name="gpt-4",
messages=[agent_msg]
)
# Create an OpenResponse containing the agent's response
open_response = OpenResponse(
response=agent_response,
prompt_hash="example_hash_123"
)
# Print the OpenResponse object (serialized to JSON)
print("OpenResponse object created:")
print(open_response.model_dump_json(indent=2))
# Example of deserialization from JSON string
json_data = open_response.model_dump_json()
reconstructed_response = OpenResponse.model_validate_json(json_data)
print("\nOpenResponse object deserialized:")
print(reconstructed_response.model_dump_json(indent=2))