chore: improve typing and docs in agents leaf files (#3461)
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled

- Add typing and Google-style docstrings to agents leaf files
- Add TODO notes
This commit is contained in:
Greyson LaLonde
2025-09-08 11:57:34 -04:00
committed by GitHub
parent fa06aea8d5
commit d5126d159b
3 changed files with 109 additions and 35 deletions

View File

@@ -1,29 +1,58 @@
"""Base converter adapter for structured output conversion."""
from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from crewai.agents.agent_adapters.base_agent_adapter import BaseAgentAdapter
from crewai.task import Task
class BaseConverterAdapter(ABC): class BaseConverterAdapter(ABC):
"""Base class for all converter adapters in CrewAI. """Abstract base class for converter adapters in CrewAI.
This abstract class defines the common interface and functionality that all Defines the common interface for converting agent outputs to structured formats.
converter adapters must implement for converting structured output. All converter adapters must implement the methods defined here.
""" """
def __init__(self, agent_adapter): def __init__(self, agent_adapter: BaseAgentAdapter) -> None:
"""Initialize the converter adapter.
Args:
agent_adapter: The agent adapter to configure for structured output.
"""
self.agent_adapter = agent_adapter self.agent_adapter = agent_adapter
@abstractmethod @abstractmethod
def configure_structured_output(self, task) -> None: def configure_structured_output(self, task: Task) -> None:
"""Configure agents to return structured output. """Configure agents to return structured output.
Must support json and pydantic output.
Must support both JSON and Pydantic output formats.
Args:
task: The task requiring structured output.
""" """
pass
@abstractmethod @abstractmethod
def enhance_system_prompt(self, base_prompt: str) -> str: def enhance_system_prompt(self, base_prompt: str) -> str:
"""Enhance the system prompt with structured output instructions.""" """Enhance the system prompt with structured output instructions.
pass
Args:
base_prompt: The original system prompt.
Returns:
Enhanced prompt with structured output guidance.
"""
@abstractmethod @abstractmethod
def post_process_result(self, result: str) -> str: def post_process_result(self, result: str) -> str:
"""Post-process the result to ensure it matches the expected format: string.""" """Post-process the result to ensure proper string format.
pass
Args:
result: The raw result from agent execution.
Returns:
Processed result as a string.
"""

View File

@@ -1,29 +1,32 @@
"""Base output converter for transforming text into structured formats."""
from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, Optional from typing import Any
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
class OutputConverter(BaseModel, ABC): class OutputConverter(BaseModel, ABC):
""" """Abstract base class for converting text to structured formats.
Abstract base class for converting task results into structured formats.
This class provides a framework for converting unstructured text into Uses language models to transform unstructured text into either Pydantic models
either Pydantic models or JSON, tailored for specific agent requirements. or JSON objects based on provided instructions and target schemas.
It uses a language model to interpret and structure the input text based
on given instructions.
Attributes: Attributes:
text (str): The input text to be converted. text: The input text to be converted.
llm (Any): The language model used for conversion. llm: The language model used for conversion.
model (Any): The target model for structuring the output. model: The target Pydantic model class for structuring output.
instructions (str): Specific instructions for the conversion process. instructions: Specific instructions for the conversion process.
max_attempts (int): Maximum number of conversion attempts (default: 3). max_attempts: Maximum number of conversion attempts (default: 3).
""" """
text: str = Field(description="Text to be converted.") text: str = Field(description="Text to be converted.")
llm: Any = Field(description="The language model to be used to convert the text.") llm: Any = Field(description="The language model to be used to convert the text.")
model: Any = Field(description="The model to be used to convert the text.") model: type[BaseModel] = Field(
description="The model to be used to convert the text."
)
instructions: str = Field(description="Conversion instructions to the LLM.") instructions: str = Field(description="Conversion instructions to the LLM.")
max_attempts: int = Field( max_attempts: int = Field(
description="Max number of attempts to try to get the output formatted.", description="Max number of attempts to try to get the output formatted.",
@@ -31,11 +34,23 @@ class OutputConverter(BaseModel, ABC):
) )
@abstractmethod @abstractmethod
def to_pydantic(self, current_attempt=1) -> BaseModel: def to_pydantic(self, current_attempt: int = 1) -> BaseModel:
"""Convert text to pydantic.""" """Convert text to a Pydantic model instance.
pass
Args:
current_attempt: Current attempt number for retry logic.
Returns:
Pydantic model instance with structured data.
"""
@abstractmethod @abstractmethod
def to_json(self, current_attempt=1) -> dict: def to_json(self, current_attempt: int = 1) -> dict[str, Any]:
"""Convert text to json.""" """Convert text to a JSON dictionary.
pass
Args:
current_attempt: Current attempt number for retry logic.
Returns:
Dictionary containing structured JSON data.
"""

View File

@@ -1,15 +1,45 @@
from typing import Any, Dict, Optional """Cache handler for tool usage results."""
from typing import Any
from pydantic import BaseModel, PrivateAttr from pydantic import BaseModel, PrivateAttr
class CacheHandler(BaseModel): class CacheHandler(BaseModel):
"""Callback handler for tool usage.""" """Handles caching of tool execution results.
_cache: Dict[str, Any] = PrivateAttr(default_factory=dict) Provides in-memory caching for tool outputs based on tool name and input.
def add(self, tool, input, output): Notes:
- TODO: Make thread-safe.
"""
_cache: dict[str, Any] = PrivateAttr(default_factory=dict)
def add(self, tool: str, input: str, output: Any) -> None:
"""Add a tool result to the cache.
Args:
tool: Name of the tool.
input: Input string used for the tool.
output: Output result from tool execution.
Notes:
- TODO: Rename 'input' parameter to avoid shadowing builtin.
"""
self._cache[f"{tool}-{input}"] = output self._cache[f"{tool}-{input}"] = output
def read(self, tool, input) -> Optional[str]: def read(self, tool: str, input: str) -> Any | None:
"""Retrieve a cached tool result.
Args:
tool: Name of the tool.
input: Input string used for the tool.
Returns:
Cached result if found, None otherwise.
Notes:
- TODO: Rename 'input' parameter to avoid shadowing builtin.
"""
return self._cache.get(f"{tool}-{input}") return self._cache.get(f"{tool}-{input}")