If a model or provider does not natively support tool calls or structured outputs (like Pydantic models), use ToolSupportProvider from g4f.providers.tool_support. This acts as a bridge that ensures the agent can format raw or unstructured model responses into the expected structured format (e.g., a Pydantic BaseModel).
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models import ModelSettings
from g4f.integration.pydantic_ai import AIModel
from g4f.providers.tool_support import ToolSupportProvider
class MyModel(BaseModel):
city: str
country: str
agent = Agent(
AIModel(
"OpenaiChat:gpt-4o",
ToolSupportProvider
),
result_type=MyModel,
model_settings=ModelSettings(temperature=0)
)
result = agent.run_sync('European city with the bear.')
print(result.data)
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models import ModelSettings
from g4f.integration.pydantic_ai import AIModel
from g4f.providers.tool_support import ToolSupportProvider
# Define a custom model for structured output
class MyModel(BaseModel):
city: str
country: str
# Create the agent using ToolSupportProvider to bridge the gap
agent = Agent(AIModel(
"OpenaiChat:gpt-4o",
ToolSupportProvider
), result_type=MyModel, model_settings=ModelSettings(temperature=0))
if __name__ == '__main__':
result = agent.run_sync('European city with the bear.')
print(result.data) # Structured output of city and country
print(result.usage()) # Usage statistics