Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid JSON response when using Claude due to unescaped quotes #2128

Open
rpvitruvix opened this issue Feb 14, 2025 · 3 comments
Open

Invalid JSON response when using Claude due to unescaped quotes #2128

rpvitruvix opened this issue Feb 14, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@rpvitruvix
Copy link

When using Claude with structured output, I'm getting JSON parsing errors due to unescaped quotes in string values. I know this might be an Anthropic issue, but wanted to check if there's anything we can do on the Agno side?

Example error:

ERROR:__main__:Failed to process document chunk: Expecting ',' delimiter: line 6 column 162 (char 265)

The issue happens in responses like this:

"surrounding_text": "...this power of attorney would be a "Springing Power of Attorney" in that..."

Any suggestions on how to handle this?

@dirkbrnd dirkbrnd added the bug Something isn't working label Feb 15, 2025
@manthanguptaa
Copy link
Contributor

manthanguptaa commented Feb 17, 2025

Hey @rpvitruvix! I’d love to help you out with this. I tried running the example from our cookbook myself

from typing import List

from agno.agent import Agent, RunResponse  # noqa
from agno.models.anthropic import Claude
from pydantic import BaseModel, Field
from rich.pretty import pprint  # noqa


class MovieScript(BaseModel):
    setting: str = Field(
        ..., description="Provide a nice setting for a blockbuster movie."
    )
    ending: str = Field(
        ...,
        description="Ending of the movie. If not available, provide a happy ending.",
    )
    genre: str = Field(
        ...,
        description="Genre of the movie. If not available, select action, thriller or romantic comedy.",
    )
    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. Make it exciting!"
    )


movie_agent = Agent(
    model=Claude(id="claude-3-5-sonnet-20240620"),
    description="You help people write movie scripts.",
    response_model=MovieScript,
)

# Get the response in a variable
run: RunResponse = movie_agent.run("New York")
pprint(run.content)

and everything worked fine on my end. Could you let me know if there’s something specific you’re trying out? I’m happy to troubleshoot with you and get this sorted!

@rpvitruvix
Copy link
Author

rpvitruvix commented Feb 17, 2025

Thank you for your response. I apologize for not being specific enough in my initial report. Let me clarify:

The issue occurs intermittently when using structured output - not always. Specifically, when Claude includes quotes (") within string values in the JSON response, these quotes sometimes appear unescaped, which breaks JSON parsing. For example:

{
  "surrounding_text": "...this power of attorney would be a "Springing Power of Attorney" in that..."
}

Note the unescaped quotes around "Springing Power of Attorney" - this causes JSON parsing errors. The correct JSON format should escape these internal quotes like this:

{
  "surrounding_text": "...this power of attorney would be a \"Springing Power of Attorney\" in that..."
}

I want to emphasize that this is not a consistent issue - it happens intermittently. I'm raising this here to see if it's a known issue and if there might be a way to prevent it through prompting.

@dirkbrnd
Copy link
Contributor

Thanks @rpvitruvix, we'll look into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants