{"record":{"id":"7b47088051d808fe","repo":"assafelovic/gpt-researcher","slug":"cost-must-be-an-integer-or-float","errorCode":null,"errorMessage":"Cost must be an integer or float","messagePattern":"Cost must be an integer or float","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"gpt_researcher/agent.py","lineNumber":785,"sourceCode":"\n        Args:\n            verbose: Whether to enable verbose output.\n        \"\"\"\n        self.verbose = verbose\n\n    def add_costs(self, cost: float) -> None:\n        \"\"\"Add to the accumulated API costs.\n\n        The cost is attributed to the current step set via ``_current_step``.\n\n        Args:\n            cost: Cost amount to add in USD.\n\n        Raises:\n            ValueError: If cost is not a number.\n        \"\"\"\n        if not isinstance(cost, (float, int)):\n            raise ValueError(\"Cost must be an integer or float\")\n        self.research_costs += cost\n        step = self._current_step\n        self.step_costs[step] = self.step_costs.get(step, 0.0) + cost\n        if self.log_handler:\n            self._log_event(\"research\", step=\"cost_update\", details={\n                \"cost\": cost,\n                \"total_cost\": self.research_costs,\n                \"step_name\": step,\n            })\n","sourceCodeStart":767,"sourceCodeEnd":795,"githubUrl":"https://github.com/assafelovic/gpt-researcher/blob/6f998577d547b1e54ec662dac63583aa11e3b84b/gpt_researcher/agent.py#L767-L795","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nagent.add_costs(response[\"cost\"])  # \"0.02\" string\n// after\nagent.add_costs(float(response[\"cost\"]))","handlingStrategy":"type-guard","validationCode":"cost = float(cost) if cost is not None else 0.0","typeGuard":"def is_numeric_cost(c) -> bool:\n    return isinstance(c, (int, float)) and not isinstance(c, bool)","tryCatchPattern":"try:\n    agent.add_costs(cost)\nexcept ValueError:\n    agent.add_costs(float(cost))","preventionTips":["Always coerce LLM/config-derived cost values to float before calling add_costs","Default missing costs to 0.0"],"tags":["python","type-validation","cost-tracking"],"backgroundTag":"invalid-argument-type","analyzedSha":"6f998577d547b1e54ec662dac63583aa11e3b84b","analyzedAt":"2026-08-28T17:50:07.383Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}