The relevant identity is $$\langle x| \hat{p}|\psi\rangle =−i \hbar \frac{d}{dx}\langle x|\psi\rangle\tag{1}$$ which is nothing but the definition of the operator $\hat{p}$.
Instead $\frac{d}{dx}|\psi\rangle$ does not make sense as it stands. Because $\frac{d}{dx}$ acts on functions of $x$ whereas $|\psi\rangle$ is a vector in a Hilbert space. Conversely $\langle x|\psi \rangle = \psi (x)$ defines a function of $x$ when $x$ varies in $\mathbb R$
so that $\frac{d}{dx}\langle x|\psi\rangle$ makes sense.
The operator $\hat{p}$ must be viewed as an operator working on abstract vectors of the abstract Hilbert space $\cal H$ and it has different equivalent definitions depending on the representation you fix for $\cal H$ in terms of a Hilbert space of functions. As a matter of fact, if dealing with the so-called position representation, i.e., representing the vectors $|\psi\rangle$ in terms of wavefunctions $\psi= \psi(x)$, the corresponding Hilbert space isomorphism is
$$U: \cal H \ni |\psi\rangle \mapsto \psi\quad \mbox{with $\psi(x) = \langle x|\psi \rangle$ for $x \in \mathbb R$}\:.$$
That is a Hilbert-space isomorphism from $\cal H$ to $L^2(\mathbb R, dx)$. Definition (1) can be rephrased as
$$\hat{p} = U \left(-i \hbar \frac{d}{dx}\right)\: U^{-1}$$
and it holds, obviously, when dealing with a suitably smooth domain of functions in $L^2(\mathbb R, dx)$.
An alternate equivalent definition is obtained by dealing with the so-called momentum representation, where the wavefunctions are function of the momentum values: $\hat{\psi}= \hat{\psi}(p)$. Here,
$$\hat{p} = V p \: V^{-1}\tag{2}$$
where $p$ in the right-hand side is the multiplicative operator and
$$V: \cal H \ni |\psi\rangle \mapsto \hat\psi\quad \mbox{with $\hat{\psi}(p) = \langle p|\psi \rangle$ for $p \in \mathbb R$.}$$
Above, $$\langle p|\psi \rangle = \frac{1}{\sqrt{2\pi \hbar}}\int_{\mathbb R} e^{-i\frac{px}{\hbar}}\psi(x) dx$$ and $V : {\cal H} \to L^2(\mathbb R, dp)$ is another Hilbert-space isomorphism.
Clearly, here $VU^{-1} : L^2(\mathbb R, dx) \to L^2(\mathbb R, dp)$ is the Fourier transform.
Within this framework, another way to write (2) is
$$\langle p| \hat{p}|\psi\rangle =p\langle p|\psi\rangle\tag{3}\:.$$