assafelovic/gpt-researcher · error · ValueError
Cost must be an integer or float
Error message
Cost must be an integer or float
What it means
Raised by GPTResearcher.add_costs when the cost argument is not a float or int (e.g. a string like '0.02' or None). The method accumulates research_costs and per-step step_costs, so it requires a real number to keep the arithmetic valid. It is a simple type precondition check.
Source
Thrown at gpt_researcher/agent.py:785
Args:
verbose: Whether to enable verbose output.
"""
self.verbose = verbose
def add_costs(self, cost: float) -> None:
"""Add to the accumulated API costs.
The cost is attributed to the current step set via ``_current_step``.
Args:
cost: Cost amount to add in USD.
Raises:
ValueError: If cost is not a number.
"""
if not isinstance(cost, (float, int)):
raise ValueError("Cost must be an integer or float")
self.research_costs += cost
step = self._current_step
self.step_costs[step] = self.step_costs.get(step, 0.0) + cost
if self.log_handler:
self._log_event("research", step="cost_update", details={
"cost": cost,
"total_cost": self.research_costs,
"step_name": step,
})
View on GitHub (pinned to 6f998577d5)
Solutions
- Convert before calling: add_costs(float(cost)) or add_costs(Decimal->float)
- If the value may be missing, default it: add_costs(cost or 0.0)
- Wrap the call in try/except ValueError if cost provenance is untrusted
Example fix
// before agent.add_costs(response["cost"]) # "0.02" string // after agent.add_costs(float(response["cost"]))
Defensive patterns
Strategy: type-guard
Validate before calling
cost = float(cost) if cost is not None else 0.0
Type guard
def is_numeric_cost(c) -> bool:
return isinstance(c, (int, float)) and not isinstance(c, bool) Try / catch
try:
agent.add_costs(cost)
except ValueError:
agent.add_costs(float(cost)) Prevention
- Always coerce LLM/config-derived cost values to float before calling add_costs
- Default missing costs to 0.0
When it happens
Trigger: Calling agent.add_costs('0.5'), add_costs(None), or passing a Decimal/numpy type is fine only for int/float—strings and None raise. Common when cost is parsed from a JSON/config value that arrived as a string.
Common situations: Reading cost from an LLM response or env var as a string; computing costs from a dict like response.get('cost') that returns None; passing Decimal from a money library.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Invalid type for path. Expected str, bytes, os.PathLike, or
- Embedding provider not found.
- Invalid retriever(s) found: {', '.join(invalid_retrievers)}.
- Set SMART_LLM or FAST_LLM = '<llm_provider>:<llm_model>' Eg
- Invalid reasoning effort: {reasoning_effort_str}. Valid opti
AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28).
Data as JSON: /api/errors/7b47088051d808fe.
Report an issue: GitHub.