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

anthropic structured output does not work #30158

Open
5 tasks done
berkbirkan opened this issue Mar 7, 2025 · 0 comments
Open
5 tasks done

anthropic structured output does not work #30158

berkbirkan opened this issue Mar 7, 2025 · 0 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@berkbirkan
Copy link

berkbirkan commented Mar 7, 2025

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

@Retry()
def get_keywords_and_title(
post_content: str,
lang: str,
openai_api_key: str = None,
anthropic_api_key: str = None,
deepseek_api_key: str = None,
primary_llm: str = 'openai',
secondary_llm: str = 'anthropic'
) -> PostKeywords:
try:
# API anahtarlarını ayarla (parametre gelmezse varsayılan değer kullanılır)
openai_api_key_const = openai_api_key or "sk-"
anthropic_api_key_const = anthropic_api_key or "sk-"
deepseek_api_key_const = deepseek_api_key or "sk-"

    # Prompt şablonunu tanımlıyoruz.
    prompt_template = PromptTemplate(
        input_variables=["post_content", "lang"],
        template=(
            "You are an expert SEO content writer. Your task is to generate the following for the provided post content:\n"
            "1. A focus keyword.\n"
            "2. A list of 3-5 SEO-friendly related keywords.\n"
            "3. An SEO-optimized post title (50-60 characters) starting with the focus keyword.\n"
            "4. An SEO-friendly meta description (150-160 characters) containing the focus keyword and related keywords.\n"
            "The output must be in the {lang} language.\n"
            "Here is the content of the post: '''{post_content}'''"
        )
    )
    prompt = prompt_template.invoke({"post_content": post_content, "lang": lang})

    # Yardımcı fonksiyon: Belirtilen LLM türüne göre ilgili LLM örneğini döndürür.
    def get_llm_instance(llm_type: str):
        if llm_type.lower() == 'openai':
            return ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key_const)
        elif llm_type.lower() == 'anthropic':
            return ChatAnthropic(model="claude-3-5-sonnet-20240620", max_retries=5, api_key=anthropic_api_key_const)
        elif llm_type.lower() == 'deepseek':
            return ChatDeepSeek(model="deepseek-r1", api_key=deepseek_api_key_const)
        else:
            raise ValueError(f"Invalid LLM type provided: {llm_type}")

    # Primary ve secondary LLM örneklerini oluştur ve fallback mekanizmasını kur.
    llm_primary = get_llm_instance(primary_llm)
    llm_secondary = get_llm_instance(secondary_llm)
    llm = llm_primary.with_fallbacks([llm_secondary])

    # LangChain'in with_structured_output yöntemiyle, JSON Schema tabanlı yapılandırılmış çıktıyı elde ediyoruz.
    structured_llm = llm.with_structured_output(PostKeywords,method="json_schema")
    result = structured_llm.invoke(prompt)

    return result

except Exception as e:
    raise Exception(f"An error occurred while extracting keywords, title, and meta description: {str(e)}")

Error Message and Stack Trace (if applicable)

7028573 2025-03-06T00:57:20.766581 http://localhost [email protected]
An error occurred while translating the text: 1 validation error for Translation translated_text Field required [type=missing, input_value={}, input_type=dict] For further information visit https://errors.pydantic.dev/2.9/v/missing 7028572 2025-03-06T00:57:20.758313 http://localhost
An error occurred while translating the text: 1 validation error for Translation translated_text Field required [type=missing, input_value={}, input_type=dict] For further information visit https://errors.pydantic.dev/2.9/v/missing 7028476 2025-03-06T00:56:12.897595 http://localhost
An error occurred while translating the text: 1 validation error for Translation translated_text Field required [type=missing, input_value={}, input_type=dict] For further information visit https://errors.pydantic.dev/2.9/v/missing

Description

While openai produces structured output without any problems in the function, I get an error when I try to use anthropic as LLM. I am 100% sure that the problem is not related to the credit or the API key. The problem may be that the model does not support structured output. However, it is written in the langchain documentation that it does. Also, when I tried it months ago, I was able to get output without any problems. What may have changed in the last few months?

System Info

requirements.txt :

Flask==3.0.3
Flask_Admin==1.6.1
flask_sqlalchemy==3.1.0
psycopg2-binary==2.9.8
Flask-WTF==1.2.1
Werkzeug==3.0.4
Flask-Migrate==4.0.7
Flask-Login==0.6.3
stripe==11.2.0
wtforms==3.1.2
wtforms_sqlalchemy==0.4.1
requests==2.32.3
Pillow==11.0.0
Flask-Cors==5.0.0
certifi==2024.8.30
wordpress-client==0.1.5
fake-useragent==1.5.1
playwright==1.48.0
langchain==0.3.7
langchain-community==0.3.7
langchain-core==0.3.40
unstructured==0.16.5
flask[async]
playwright-recaptcha==0.5.1
openai>=1.58.1,<2.0.0
pydantic==2.9.2
beautifulsoup4==4.12.3
feedparser==6.0.11
langchain-openai==0.3.7
duckduckgo-search==6.3.5
fal-client==0.5.6
Flask-Limiter==3.8.0
phpserialize==1.3
langchain-anthropic==0.3.0
agno==1.1.7
yfinance==0.2.49
wikipedia==1.4.0
runware==0.3.5
mailersend==0.5.8
numpy
scikit-learn
email_validator==2.2.0
langchain-deepseek==0.1.2

dockerfile:

FROM python:3.10-slim

Çalışma dizini oluştur

WORKDIR /app

Sadece requirements.txt dosyasını kopyalayın

COPY requirements.txt .

Gerekli bağımlılıkları yükle

RUN pip install --no-cache-dir -r requirements.txt
RUN playwright install
RUN playwright install-deps

Uygulama dosyalarını kopyala

COPY . .

Flask uygulamasını başlat

CMD ["python", "app.py"]

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant