AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

`BaseTool.Model_dump(Mode="Json")` Fails On `Args_schema`, `Func`, And `Coroutine` Fields

BaseTool.model_dump(mode='json') raises PydanticSerializationError because args_schema (a class), func, and coroutine are not JSON-serializable. This produces noisy debug logs in langsmith tracing and breaks direct JSON serialization.

mediumConfidence 95%Langchain-CoreChat ModelsMemoryAffected V*

Origin Analysis

Pydantic cannot serialize ModelMetaclass (class) and Callable objects by default. BaseTool defines these fields without custom serializers, causing model_dump(mode='json') to fail.
```python from langchain_core.tools import StructuredTool from pydantic import BaseModel class Input(BaseModel): x: str def my_func(x: str) -> str: return x tool = StructuredTool.from_function(func=my_func, name="test", description="test", args_schema=Input) tool.model_dump(mode="json", exclude_none=True) ```

Fixing Code Block

```python from pydantic import field_serializer class BaseTool(...): @field_serializer("args_schema", when_used="json") def serialize_args_schema(self, args_schema, _info): if args_schema is None: return None return getattr(args_schema, "__name__", str(args_schema)) @field_serializer("func", "coroutine", when_used="json") def serialize_callable(self, callable_obj, _info): if callable_obj is None: return None return getattr(callable_obj, "__name__", str(callable_obj)) ```
Add Pydantic field serializers that convert non-serializable fields to safe JSON values (class name for args_schema and function name for callables) when mode='json' is used. This allows model_dump(mode='json') to succeed.

Edge Case Audit

Returning only class/function names loses detail: downstream consumers expecting full JSON schema or callable reference will get strings. If users rely on complete serialization, consider returning model_json_schema() for args_schema instead. The fix is backward compatible and does not affect non-JSON modes. To roll back, remove the decorators.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state