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

context_length_exceeded error #837

Open
infinitivebyte opened this issue Feb 1, 2025 · 15 comments
Open

context_length_exceeded error #837

infinitivebyte opened this issue Feb 1, 2025 · 15 comments
Labels
question Further information is requested Stale

Comments

@infinitivebyte
Copy link

infinitivebyte commented Feb 1, 2025

I think the message history keeps building up when running the agent over a large loop like:

for keyword in keywords:
    result = lead_generation_agent.run_sync(user_promt, deps=keyword)

because the first iteration runs just fine but after dozen I get this error, why and how can I fix it?

File "/Users/UA/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1638, in _request
raise self._make_status_error_from_response(err.response) from None
openai.OpenAIError: Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 136707 tokens (136367 in the messages, 340 in the functions). Please reduce the length of the messages or functions.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}

How Agent class works, why does it pass the history messages to the next iteration of the for loop?

@izzyacademy
Copy link
Contributor

@infinitivebyte you need to find a mechanism to trim/reduce the size of the message history you are passing to the run/run_sync method. You can chose to use a subset of the list or create a helper function to summarize the messages and update the history with a summarize version of the history that is less than the maximum context length for your language model.

@infinitivebyte
Copy link
Author

@izzyacademy my wonder is why the history for processing the first keyword on the list passed to the second keyword?

when i do result = lead_generation_agent.run_sync(user_promt, deps=keyword) this does not instantiate new lead_generetation_agent with 0 message history, if not how do just do that?

@grassxyz
Copy link

grassxyz commented Feb 3, 2025 via email

@infinitivebyte
Copy link
Author

@grassxyz sad new is that approach didnt work neither. I am still cant figure out why the messages keeps accumulating in each iteration. my brain kind melted around this.

right now switching to pure openai api using function calling until maybe maintainers jumps in and explain

@grassxyz
Copy link

grassxyz commented Feb 3, 2025 via email

@infinitivebyte
Copy link
Author

result = fresh_agent.run_sync("what is contact info", deps=keyword, message_history=[]) does not seems to work neither. am I doing something wrong?

@grassxyz
Copy link

grassxyz commented Feb 3, 2025 via email

@infinitivebyte
Copy link
Author

@grassxyz here is my full code, so after dozen iteration i start to get context_length_exceeded error. any thoughts? thanks


from typing import Optional, List

import logfire
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel, EmailStr, Field
from loguru import logger as log

from googlesearch.spider import GoogleSearch
from googlesearch.utils.fetch_search_result_url import get_markdown


logfire.configure()


class SearchDependency(BaseModel):
    city: str
    title: str


class GoogleSearchResult(BaseModel):
    url: Optional[str] = Field("", description="URL of the search result")
    snippet: Optional[str] = Field("", description="Snippet of the search result")


class Lead(BaseModel):
    """
    Lead model to store the contact information.
    """

    first_name: str
    last_name: str
    email: Optional[EmailStr] = Field(None, description="Email address of the lead")


lead_generation_agent = Agent(
    "openai:gpt-4o-mini",
    system_prompt=system_prompt,
    result_type=Lead,
)


@lead_generation_agent.tool
def get_city_position(ctx: RunContext[SearchDependency]) -> str:
    city = ctx.deps.city
    title = ctx.deps.title
    return f"The city is {city} and the position is {title}"


@lead_generation_agent.tool(retries=8)
async def google_search(
    ctx: RunContext[None], location: str, title: str
) -> List[GoogleSearchResult]:
    data = []
    keyword = f"{location} {title}"
    search = GoogleSearch(keyword)
    items = search.run()
    for item in items:
        data.append(GoogleSearchResult(**item))
    return data


@lead_generation_agent.tool(retries=5)
async def fetch_url(ctx: RunContext[None], url: str) -> str:
    mark = get_markdown(url)
    return mark

for keyword in keywords:
    keyword = SearchDependency(**keyword)
    result = lead_generation_agent.run_sync("what is contact info", deps=keyword, message_history=[], infer_name=False)
    log.success(result.data)

@grassxyz
Copy link

grassxyz commented Feb 3, 2025 via email

@infinitivebyte
Copy link
Author

    system_prompt=(
        """
    You are an experienced lead generation specialist.
    You are asked to find and compile the contact information of the specific position/title for a given city:
    - first name
    - last name
    - email
    - title
    - oficial city website
    - phone
    - address
    - state
    - zip code
    - direct contact form link
    - source from where the lead was collected.
    
    user google_search tool to search for the contact information of the specific position/title for a given city.
    """
    )

retries set to 1

added description/docsctring to each function, but still getting that context_limit error after dozen of iteration.

@grassxyz
Copy link

grassxyz commented Feb 3, 2025 via email

@grassxyz
Copy link

grassxyz commented Feb 3, 2025 via email

@sydney-runkle sydney-runkle added the question Further information is requested label Feb 4, 2025
@infinitivebyte
Copy link
Author

I highly suspect that this is a prompt issue. Regardless of prompt the message history accumulation within the loop, defeats the stateless claim of an Agent in the documentation.

@grassxyz
Copy link

grassxyz commented Feb 4, 2025 via email

Copy link

This issue is stale, and will be closed in 3 days if no reply is received.

@github-actions github-actions bot added the Stale label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

4 participants