Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix: use object.__setattr__ for LLM ainvoke patching to avoid pydantic crash

Laith Weinberger3242d9dbc21b
diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.py
index 081ab599f..8c3a25fae 100644
--- a/browser_use/tokens/service.py
+++ b/browser_use/tokens/service.py
@@ -367,9 +367,9 @@ class TokenCost:
return result
- # Replace the method with our tracked version
- # Using setattr to avoid type checking issues with overloaded methods
- setattr(llm, 'ainvoke', tracked_ainvoke)
+ # Replace the method with our tracked version.
+ # Use setattr so Pydantic-backed models don't reject runtime patch
+ object.__setattr__(llm, 'ainvoke', tracked_ainvoke)
return llm