Documentation
The ContentParsingAgent class is an agent dedicated to processing structured input and output data, automatically generating Pydantic schemas to validate that data, as well as orchestrating the execution of configurable language models to process the content according to the given instructions.
On this page
#Overview
The ContentParsingAgent class is an agent dedicated to processing structured input and output data, automatically generating Pydantic schemas to validate that data, as well as orchestrating the execution of configurable language models to process the content according to the given instructions. This allows a smooth integration between the data used and the artificial intelligence models, ensuring consistency and rigorous validation of the information.
It solves the problem of converting raw data into typed and validated models, as well as making it easier to run and control language models from different providers, such as OpenAI or Groq, without the user having to deal directly with the internal details of those models. In practice, the class can be used to automate text data processing pipelines and content generation based on defined rules and configurations, all while ensuring reliability and traceability of the flow.
#Execution Flow
- Initialization: The user creates an instance of
ContentParsingAgentpassing the input and output data as dictionaries, plus optional settings. - Schema Generation: Internally, the class generates Pydantic schemas for the input, output and configuration data to ensure the validity and typing of that information.
- Model Configuration: Based on the configuration, the class selects a suitable language model (e.g. OpenAI or Groq).
- Agent Execution: With the instructions, description, settings and schemas ready, the agent runs the model over the input data.
- Response Formatting: The raw model response is formatted into a structured dictionary containing the processed content, model metadata and execution metrics, making it easier to use or publish.
#Class Methods Table
| Method | Description |
|---|---|
__init__ | Initializes the agent with input data, output data and config. |
get_schemas | Returns the Pydantic schemas generated for input, output and configuration. |
run_agent | Runs the agent with the configured model and returns the generated response. |
format_response | Formats the agent's raw response into a structured dictionary. |
#Environment Variables
- No specific environment variable is directly mentioned, but the presence of
load_dotenv()suggests that variables may be loaded externally for settings (example: API keys).
#Key Architecture Points and Insights
- Automatic Schema Generation: Uses specific classes (
JsonToPydantic,GeneratePydanticSchema) to convert generic data into Pydantic models, ensuring consistent validation and typing throughout the flow. - Language Model Flexibility: Adopts a simple factory approach to return different model implementations (OpenAI, Groq) based on the settings, which makes it easier to extend to other providers.
- Encapsulation and Error Handling: Centralizes critical operations (schema generation, agent execution, response formatting) with try-except blocks that convert various errors into specific exceptions, increasing robustness.
- External Dependencies: Interacts with external classes for models (
Agent,OpenAIResponses,Groq) and parsing (Config,JsonToPydantic,GeneratePydanticSchema), delegating responsibilities and keeping the class focused on the main flow.
#Class and Methods Description
#ContentParsingAgent Class
#Description
This class works as an intermediary agent to process structured data, converting it into validated Pydantic models and using configured language models to process text data according to specific instructions. It offers an integrated pipeline that covers everything from validation to execution and response formatting, making integrations with AI systems easier in a safe and modular way.
#Constructor Arguments
| Argument | Type | Description | Default Value |
|---|---|---|---|
| input_data | Dict[str, Any] | Dictionary with the input data to be validated and processed. | None |
| output_data | Dict[str, Any] | Dictionary with the output data expected after processing. | None |
| config_data | Optional[Dict[str,Any]] | Optional dictionary containing the agent's settings. | None |
#1. __init__
Description
Initializes the agent with the input data, output data and optional settings, and immediately generates the Pydantic schemas needed to validate that data.
Arguments
input_data(Dict[str, Any]): Input data to be processed.output_data(Dict[str, Any]): Desired output data.config_data(Optional[Dict[str, Any]]): Additional settings for the agent (optional).
Returns
- Returns no value.
Raises
- None explicitly, but it may raise RuntimeError during schema generation.
Examples
agent = ContentParsingAgent(
input_data={"nome": "João", "idade": 30},
output_data={"resultado": "string"},
config_data={"model_provider": "openai", "model_id": "gpt-4"}
)#2. get_schemas
Description
Returns the Pydantic schemas corresponding to the input, output and configuration data generated by the agent.
Arguments
- None.
Returns
- dict: Dictionary with the keys
"input","output"and"config"containing their respective schemas.
Raises
- None.
Examples
schemas = agent.get_schemas()
print(schemas["input"])
print(schemas["output"])
print(schemas["config"])#3. run_agent
Description
Runs the agent using the configured model and the defined instructions to process the input data, returning the generated response.
Arguments
- None.
Returns
- Object of the response generated by the model, usually containing content and metrics.
Raises
- RuntimeError: If an error occurs during model execution.
Examples
response = agent.run_agent()
print(response)#4. format_response
Description
Formats the raw response obtained from running the agent into a structured dictionary, extracting the useful content, model metadata and execution statistics for easy consumption.
Arguments
raw_response: Raw response object returned by the agent.
Returns
- dict: Structured dictionary with the keys
"content"(processed data) and"metadata"(model information and metrics).
Raises
- RuntimeError: If an error occurs during response formatting.
Examples
formatted = agent.format_response(response)
print(formatted["content"])
print(formatted["metadata"]["model"])
print(formatted["metadata"]["tokens"])
# python -m src.content_parse.content_parsing_agentThis documentation presents ContentParsingAgent in a didactic and structured way to make it easier for developers to understand and use it, highlighting the key points of how it works and practical application examples.
Source: src/content_parse/content_parsing_agent.py