Does Agno care that some models might have tons of text appended to the final json it returns? Ie - model response “Here is the data you requested: ```json…”
I assume agno will try to extract the json from wherever it is, even if it doesn’t start with ```json?
I read the docs… This doesn’t answer my question. I see the use_json_mode as a fallback, but again this doesn’t confirm what I asked above. Is it basically saying agno will try to resolve my response model as best it could? And if yes, can it handle the scenario I asked about? (A response with some other text information etc.?
Great question! Yes, Agno absolutely handles this scenario gracefully. The framework is designed to extract JSON from model responses even when they contain additional explanatory text.
Here’s a practical example of what Agno can handle:
Model Response:
Here is the movie script data you requested:
```json
{
"setting": "New York City rooftop",
"ending": "The hero saves the city and gets the girl",
"genre": "action",
"name": "Skyline Justice",
"characters": ["Jack Steel", "Sarah Connor", "Dr. Evil"],
"storyline": "A tough cop must stop a criminal mastermind from destroying the city using stolen technology."
}
Hope this helps with your project! Let me know if you need any adjustments to the script.
**Your Code:**
```python
class MovieScript(BaseModel):
setting: str = Field(..., description="Provide a nice setting for a blockbuster movie.")
ending: str = Field(..., description="Ending of the movie.")
genre: str = Field(..., description="Genre of the movie.")
name: str = Field(..., description="Give a name to this movie")
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(..., description="3 sentence storyline for the movie.")
agent = Agent(
model=Claude(id="anthropic.claude-3-5-sonnet-20240620-v1:0"),
response_model=MovieScript,
)
response = agent.run("Create a movie about New York")
print(type(response.content)) # <class 'MovieScript'>
print(response.content.name) # "Skyline Justice"
Agno automatically:
Detects and extracts JSON from code blocks (json)
Handles various JSON embedding patterns
Validates against your Pydantic model
Returns a properly structured object
The system has multiple fallback strategies, so it works reliably across different models and response formats. You don’t need any special configuration - just set your response_model and Agno handles the rest.
Hope this clarifies things! Feel free to reach out if you have any other questions.